Skip to content

Commit

Permalink
Test that ObjectNotContaining is as documented
Browse files Browse the repository at this point in the history
Asserts that ObjectNotContaining matches values which do not contain
_all_ of the sample's properties.

Asserts that ObjectNotContaining matches iff ObjectContaining does not
match.
  • Loading branch information
ninevra committed Nov 4, 2020
1 parent 2fa34c4 commit a4d69e1
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions packages/expect/src/__tests__/asymmetricMatchers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,65 @@ test('ObjectContaining does not mutate the sample', () => {

test('ObjectNotContaining matches', () => {
[
objectNotContaining({}).asymmetricMatch('jest'),
objectNotContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),
objectNotContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),
objectNotContaining({foo: undefined}).asymmetricMatch({}),
objectNotContaining({
first: objectNotContaining({second: {}}),
}).asymmetricMatch({first: {second: {}}}),
objectNotContaining({first: {second: {}, third: {}}}).asymmetricMatch({
first: {second: {}},
}),
objectNotContaining({first: {second: {}}}).asymmetricMatch({
first: {second: {}, third: {}},
}),
objectNotContaining({foo: 'foo', jest: 'jest'}).asymmetricMatch({
foo: 'foo',
}),
].forEach(test => {
jestExpect(test).toEqual(true);
});
});

test('ObjectNotContaining does not match', () => {
[
objectNotContaining({}).asymmetricMatch('jest'),
objectNotContaining({foo: 'foo'}).asymmetricMatch({
foo: 'foo',
jest: 'jest',
}),
objectNotContaining({foo: undefined}).asymmetricMatch({foo: undefined}),
objectNotContaining({first: {second: {}}}).asymmetricMatch({
first: {second: {}},
}),
objectNotContaining({
first: objectNotContaining({second: {}}),
first: objectContaining({second: {}}),
}).asymmetricMatch({first: {second: {}}}),
objectNotContaining({}).asymmetricMatch(null),
objectNotContaining({}).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(false);
});
});

test('ObjectNotContaining inverts ObjectContaining', () => {
[
[{}, null],
[{foo: 'foo'}, {foo: 'foo', jest: 'jest'}],
[{foo: 'foo', jest: 'jest'}, {foo: 'foo'}],
[{foo: undefined}, {foo: undefined}],
[{foo: undefined}, {}],
[{first: {second: {}}}, {first: {second: {}}}],
[{first: objectContaining({second: {}})}, {first: {second: {}}}],
[{first: objectNotContaining({second: {}})}, {first: {second: {}}}],
[{}, {foo: undefined}],
].forEach(([sample, received]) => {
jestExpect(objectNotContaining(sample).asymmetricMatch(received)).toEqual(
!objectContaining(sample).asymmetricMatch(received),
);
});
});

test('ObjectNotContaining throws for non-objects', () => {
jestExpect(() => objectNotContaining(1337).asymmetricMatch()).toThrow();
});
Expand Down

0 comments on commit a4d69e1

Please sign in to comment.