|
| 1 | +import {assert, expect} from '../index.js'; |
| 2 | + |
| 3 | +describe('plain object', function () { |
| 4 | + var testedObject = { |
| 5 | + a: 'b', |
| 6 | + c: 'd' |
| 7 | + }; |
| 8 | + |
| 9 | + it('should pass for smaller object', function () { |
| 10 | + expect(testedObject).to.containSubset({ |
| 11 | + a: 'b' |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should pass for same object', function () { |
| 16 | + expect(testedObject).to.containSubset({ |
| 17 | + a: 'b', |
| 18 | + c: 'd' |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should pass for similar, but not the same object', function () { |
| 23 | + expect(testedObject).to.not.containSubset({ |
| 24 | + a: 'notB', |
| 25 | + c: 'd' |
| 26 | + }); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('complex object', function () { |
| 31 | + var testedObject = { |
| 32 | + a: 'b', |
| 33 | + c: 'd', |
| 34 | + e: { |
| 35 | + foo: 'bar', |
| 36 | + baz: { |
| 37 | + qux: 'quux' |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + it('should pass for smaller object', function () { |
| 43 | + expect(testedObject).to.containSubset({ |
| 44 | + a: 'b', |
| 45 | + e: { |
| 46 | + foo: 'bar' |
| 47 | + } |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should pass for smaller object', function () { |
| 52 | + expect(testedObject).to.containSubset({ |
| 53 | + e: { |
| 54 | + foo: 'bar', |
| 55 | + baz: { |
| 56 | + qux: 'quux' |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should pass for same object', function () { |
| 63 | + expect(testedObject).to.containSubset({ |
| 64 | + a: 'b', |
| 65 | + c: 'd', |
| 66 | + e: { |
| 67 | + foo: 'bar', |
| 68 | + baz: { |
| 69 | + qux: 'quux' |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should pass for similar, but not the same object', function () { |
| 76 | + expect(testedObject).to.not.containSubset({ |
| 77 | + e: { |
| 78 | + foo: 'bar', |
| 79 | + baz: { |
| 80 | + qux: 'notAQuux' |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should fail if comparing when comparing objects to dates', function () { |
| 87 | + expect(testedObject).to.not.containSubset({ |
| 88 | + e: new Date() |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('circular objects', function () { |
| 94 | + var object = {}; |
| 95 | + |
| 96 | + before(function () { |
| 97 | + object.arr = [object, object]; |
| 98 | + object.arr.push(object.arr); |
| 99 | + object.obj = object; |
| 100 | + }); |
| 101 | + |
| 102 | + it('should contain subdocument', function () { |
| 103 | + expect(object).to.containSubset({ |
| 104 | + arr: [{arr: []}, {arr: []}, [{arr: []}, {arr: []}]] |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should not contain similar object', function () { |
| 109 | + expect(object).to.not.containSubset({ |
| 110 | + arr: [{arr: ['just random field']}, {arr: []}, [{arr: []}, {arr: []}]] |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('object with compare function', function () { |
| 116 | + it('should pass when function returns true', function () { |
| 117 | + expect({a: 5}).to.containSubset({a: (a) => a}); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should fail when function returns false', function () { |
| 121 | + expect({a: 5}).to.not.containSubset({a: (a) => !a}); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should pass for function with no arguments', function () { |
| 125 | + expect({a: 5}).to.containSubset({a: () => true}); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +describe('comparison of non objects', function () { |
| 130 | + it('should fail if actual subset is null', function () { |
| 131 | + expect(null).to.not.containSubset({a: 1}); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should fail if expected subset is not a object', function () { |
| 135 | + expect({a: 1}).to.not.containSubset(null); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should not fail for same non-object (string) variables', function () { |
| 139 | + expect('string').to.containSubset('string'); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('assert style of test', function () { |
| 144 | + it('should find subset', function () { |
| 145 | + assert.containsSubset({a: 1, b: 2}, {a: 1}); |
| 146 | + assert.containSubset({a: 1, b: 2}, {a: 1}); |
| 147 | + }); |
| 148 | + |
| 149 | + it('negated assert style should function', function () { |
| 150 | + assert.doesNotContainSubset({a: 1, b: 2}, {a: 3}); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe('comparison of dates', function () { |
| 155 | + it('should pass for the same date', function () { |
| 156 | + expect(new Date('2015-11-30')).to.containSubset(new Date('2015-11-30')); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should pass for the same date if nested', function () { |
| 160 | + expect({a: new Date('2015-11-30')}).to.containSubset({ |
| 161 | + a: new Date('2015-11-30') |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should fail for a different date', function () { |
| 166 | + expect(new Date('2015-11-30')).to.not.containSubset(new Date('2012-02-22')); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should fail for a different date if nested', function () { |
| 170 | + expect({a: new Date('2015-11-30')}).to.not.containSubset({ |
| 171 | + a: new Date('2012-02-22') |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should fail for invalid expected date', function () { |
| 176 | + expect(new Date('2015-11-30')).to.not.containSubset( |
| 177 | + new Date('not valid date') |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should fail for invalid actual date', function () { |
| 182 | + expect(new Date('not valid actual date')).to.not.containSubset( |
| 183 | + new Date('not valid expected date') |
| 184 | + ); |
| 185 | + }); |
| 186 | +}); |
| 187 | + |
| 188 | +describe('cyclic objects', () => { |
| 189 | + it('should pass', () => { |
| 190 | + const child = {}; |
| 191 | + const parent = { |
| 192 | + children: [child] |
| 193 | + }; |
| 194 | + child.parent = parent; |
| 195 | + |
| 196 | + const myObject = { |
| 197 | + a: 1, |
| 198 | + b: 'two', |
| 199 | + c: parent |
| 200 | + }; |
| 201 | + expect(myObject).to.containSubset({ |
| 202 | + a: 1, |
| 203 | + c: parent |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments