Merge pull request #18650 from eileenmcnaughton/perms
[civicrm-core.git] / tests / karma / unit / crmJsonComparator.js
CommitLineData
416abe87
PH
1'use strict';
2
09a09949
PH
3describe('crmJsonComparator', function() {
4 var comparator;
416abe87
PH
5
6 beforeEach(function() {
09a09949 7 module('crmJsonComparator');
416abe87
PH
8 });
9
10 beforeEach(function() {
09a09949
PH
11 inject(function(crmJsonComparator) {
12 comparator = crmJsonComparator;
c866eb5f 13 });
416abe87
PH
14 });
15
16 it('should return false when comparing different objects', function() {
09a09949 17 var result = comparator.compare({'foo': 'bar'}, {'bar': 'foo'});
416abe87
PH
18 expect(result.pass).toBe(false);
19 });
20
21 it('should return true when comparing equal objects', function() {
09a09949 22 var result = comparator.compare({'bar': 'foo'}, {'bar': 'foo'});
416abe87
PH
23 expect(result.pass).toBe(true);
24 });
25
c866eb5f 26 it('should explain what part of the comparison failed when comparing objects', function() {
09a09949 27 var result = comparator.compare({'foo': 'bar'}, {'bar': 'foo'});
416abe87
PH
28 expect(result.message).toBe('Could not find key \'bar\' in actual data at root.');
29 });
30
31 it('should handle nested objects', function() {
09a09949 32 var result = comparator.compare({'foo': {'bif': 'bam'}}, {'foo': {'bif': 'bam'}});
416abe87
PH
33 expect(result.pass).toBe(true);
34 });
35
36 it('should handle differences in nested objects', function() {
09a09949 37 var result = comparator.compare({'foo': {'bif': 'bam'}}, {'foo': {'bif': 'bop'}});
416abe87
PH
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() {
09a09949 43 var result = comparator.compare([1, 2, 3, 4], [1, 2, 3, 4]);
416abe87
PH
44 expect(result.pass).toBe(true);
45 });
46
47 it('should handle arrays with differences', function() {
09a09949 48 var result = comparator.compare([1, 2, 2, 4], [1, 2, 3, 4]);
416abe87
PH
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() {
09a09949 54 var result = comparator.compare([1, 2, {'foo': 'bar'}, 4], [1, 2, {'foo': 'bar'}, 4]);
416abe87
PH
55 expect(result.pass).toBe(true);
56 });
57
58 it('should handle nested arrays and objects with differences', function() {
09a09949 59 var result = comparator.compare([1, 2, {'foo': 'bar'}, 4], [1, 2, {'foo': 'bif'}, 4]);
416abe87
PH
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() {
09a09949 65 var result = comparator.compare({'foo': 'bar'}, [1, 2, 3]);
416abe87
PH
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() {
09a09949 71 var result = comparator.compare([1, 2, 3], {'foo': 'bar'});
416abe87
PH
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});