Skip to content

Commit 0c20840

Browse files
authored
Merge pull request #2 from aintgoin2goa/map
add nested array to map
2 parents dd7d90f + 2d0dcfb commit 0c20840

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/engine/matchers/map.test.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import matcher from './map';
33
test('return true if expected keys are empty', () => {
44
const result = matcher({}, { name: 'apple client' });
55

6-
expect(result).toMatchInlineSnapshot(`true`);
6+
expect(result).toBe(true);
77
});
88

99
test("don't match missing keys", () => {
1010
const result = matcher({ job: 'manager' }, { name: 'apple client' });
1111

12-
expect(result).toBeFalsy();
12+
expect(result).toBe(false);
1313
});
1414

1515
test('match values', () => {
1616
const result = matcher({ job: 'manager' }, { name: 'apple client', job: 'manager' });
1717

18-
expect(result).toMatchInlineSnapshot(`true`);
18+
expect(result).toBe(true);
1919
});
2020

2121
test("don't match unequal values", () => {
@@ -24,7 +24,7 @@ test("don't match unequal values", () => {
2424
{ name: 'apple client', job: 'manager', role: 'mentor', id: 1 },
2525
);
2626

27-
expect(result).toMatchInlineSnapshot(`false`);
27+
expect(result).toBe(false);
2828
});
2929

3030
test('match regex values', () => {
@@ -33,7 +33,7 @@ test('match regex values', () => {
3333
{ name: 'apple client', job: 'manager', role: 'mentor', id: 1 },
3434
);
3535

36-
expect(result).toMatchInlineSnapshot(`true`);
36+
expect(result).toBe(true);
3737
});
3838

3939
test("don't match regex values", () => {
@@ -42,5 +42,31 @@ test("don't match regex values", () => {
4242
{ name: 'apple client', job: 'manager', role: 'mentor', id: 1 },
4343
);
4444

45-
expect(result).toMatchInlineSnapshot(`false`);
45+
expect(result).toBe(false);
46+
});
47+
48+
describe('Nested data', () => {
49+
type AnyObject = { [key: string]: any };
50+
type TestCase = [testName: string, expected: AnyObject, actual: AnyObject, expectedResult: boolean];
51+
const testCases: TestCase[] = [
52+
[
53+
'match nested arrays',
54+
{ jobs: ['manager', 'salesperson', 'dogcatcher'] },
55+
{ jobs: ['manager', 'salesperson', 'dogcatcher'], name: 'Roni' },
56+
true,
57+
],
58+
[
59+
"don't match nested arrays",
60+
{ jobs: ['manager', 'salesperson', 'dogcatcher'] },
61+
{ jobs: ['manager', 'salesperson'], name: 'Roni' },
62+
false,
63+
],
64+
["don't match nested arrays to strings", { jobs: 'manager' }, { jobs: ['manager'], name: 'Roni' }, false],
65+
["don't match string to nested arrays", { jobs: ['manager'] }, { jobs: 'manager', name: 'Roni' }, false],
66+
];
67+
68+
test.each(testCases)('%s', (_: string, expected, actual, expectedResult) => {
69+
const result = matcher(expected, actual);
70+
expect(result).toEqual(expectedResult);
71+
});
4672
});

src/engine/matchers/map.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const matchKeys = (expected: { [key: string]: any }, actual: { [key: stri
1111
return true;
1212
};
1313

14-
export default (expected: { [key: string]: any }, actual: { [key: string]: any }): boolean => {
14+
const map = (expected: { [key: string]: any }, actual: { [key: string]: any }): boolean => {
1515
if (Object.keys(expected).length === 0) {
1616
return true;
1717
}
@@ -33,6 +33,9 @@ export default (expected: { [key: string]: any }, actual: { [key: string]: any }
3333

3434
// Match Values
3535
const nonMatchingValue = Object.entries(expected).some(([key, expValue]) => {
36+
if (typeof expValue === 'object' || typeof actual[key] === 'object') {
37+
return !map(expValue, actual[key]);
38+
}
3639
return !stringMatcher(actual[key], expValue);
3740
});
3841

@@ -42,3 +45,5 @@ export default (expected: { [key: string]: any }, actual: { [key: string]: any }
4245
return e;
4346
}
4447
};
48+
49+
export default map;

0 commit comments

Comments
 (0)