Merge pull request #19986 from scardinius/core-2516
[civicrm-core.git] / tests / karma / unit / crmJsonComparator.js
1 'use strict';
2
3 describe('crmJsonComparator', function() {
4 var comparator;
5
6 beforeEach(function() {
7 module('crmJsonComparator');
8 });
9
10 beforeEach(function() {
11 inject(function(crmJsonComparator) {
12 comparator = crmJsonComparator;
13 });
14 });
15
16 it('should return false when comparing different objects', function() {
17 var result = comparator.compare({'foo': 'bar'}, {'bar': 'foo'});
18 expect(result.pass).toBe(false);
19 });
20
21 it('should return true when comparing equal objects', function() {
22 var result = comparator.compare({'bar': 'foo'}, {'bar': 'foo'});
23 expect(result.pass).toBe(true);
24 });
25
26 it('should explain what part of the comparison failed when comparing objects', function() {
27 var result = comparator.compare({'foo': 'bar'}, {'bar': 'foo'});
28 expect(result.message).toBe('Could not find key \'bar\' in actual data at root.');
29 });
30
31 it('should handle nested objects', function() {
32 var result = comparator.compare({'foo': {'bif': 'bam'}}, {'foo': {'bif': 'bam'}});
33 expect(result.pass).toBe(true);
34 });
35
36 it('should handle differences in nested objects', function() {
37 var result = comparator.compare({'foo': {'bif': 'bam'}}, {'foo': {'bif': 'bop'}});
38 expect(result.pass).toBe(false);
39 expect(result.message).toBe("Expected 'bam' to be 'bop' at root[foo][bif].");
40 });
41
42 it('should handle arrays', function() {
43 var result = comparator.compare([1, 2, 3, 4], [1, 2, 3, 4]);
44 expect(result.pass).toBe(true);
45 });
46
47 it('should handle arrays with differences', function() {
48 var result = comparator.compare([1, 2, 2, 4], [1, 2, 3, 4]);
49 expect(result.pass).toBe(false);
50 expect(result.message).toBe("Expected '2' to be '3' at root[2].");
51 });
52
53 it('should handle nested arrays and objects', function() {
54 var result = comparator.compare([1, 2, {'foo': 'bar'}, 4], [1, 2, {'foo': 'bar'}, 4]);
55 expect(result.pass).toBe(true);
56 });
57
58 it('should handle nested arrays and objects with differences', function() {
59 var result = comparator.compare([1, 2, {'foo': 'bar'}, 4], [1, 2, {'foo': 'bif'}, 4]);
60 expect(result.pass).toBe(false);
61 expect(result.message).toBe("Expected 'bar' to be 'bif' at root[2][foo].");
62 });
63
64 it('should complain when comparing an object to an array', function() {
65 var result = comparator.compare({'foo': 'bar'}, [1, 2, 3]);
66 expect(result.pass).toBe(false);
67 expect(result.message).toBe("The expected data has an array at root, but the actual data has something else ([object Object])");
68 });
69
70 it('should complain when comparing an array to an object', function() {
71 var result = comparator.compare([1, 2, 3], {'foo': 'bar'});
72 expect(result.pass).toBe(false);
73 expect(result.message).toBe("The expected data has an object at root, but the actual data has something else (1,2,3)");
74 });
75
76 });