Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored matchFilter to use named args (via an object) #2525

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/little-geese-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/test-utils': major
'@keystonejs/fields': patch
---

Refactored `matchFilter` to use named args (via an object) which makes understanding test code easier.
10 changes: 8 additions & 2 deletions packages/fields/src/types/CalendarDay/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) =>
matchFilter(keystone, filter, '{ name, birthday }', targets, 'name');
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'name birthday',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
10 changes: 8 additions & 2 deletions packages/fields/src/types/Checkbox/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) =>
matchFilter(keystone, filter, '{ name enabled }', targets, 'name');
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'name enabled',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
10 changes: 8 additions & 2 deletions packages/fields/src/types/DateTime/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets, forceSortBy = 'name') =>
matchFilter(keystone, filter, '{ name, lastOnline }', targets, forceSortBy);
const match = (keystone, queryArgs, expected, forceSortBy = 'name') =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'name lastOnline',
expected,
sortKey: forceSortBy,
});

test(
'No filter',
Expand Down
10 changes: 8 additions & 2 deletions packages/fields/src/types/Decimal/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) =>
matchFilter(keystone, filter, '{ name, price }', targets, 'name');
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'name price',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
10 changes: 8 additions & 2 deletions packages/fields/src/types/Float/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) =>
matchFilter(keystone, filter, '{ name, stars }', targets, 'name');
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'name stars',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
10 changes: 8 additions & 2 deletions packages/fields/src/types/Integer/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) =>
matchFilter(keystone, filter, '{ name, count }', targets, 'name');
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'name count',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
14 changes: 7 additions & 7 deletions packages/fields/src/types/Password/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) =>
matchFilter(
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
filter,
'{ name password_is_set }',
targets,
'name' // Sort by name
);
queryArgs,
fieldSelection: 'name password_is_set',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
14 changes: 7 additions & 7 deletions packages/fields/src/types/Select/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ export const initItems = () => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) =>
matchFilter(
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
filter,
'{ company name }',
targets,
'name' // Sort by name
);
queryArgs,
fieldSelection: 'name company',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
11 changes: 8 additions & 3 deletions packages/fields/src/types/Text/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ export const initItems = () => {
// See https://github.com/keystonejs/keystone-5/issues/391

export const filterTests = withKeystone => {
const match = (keystone, gqlArgs, targets) => {
return matchFilter(keystone, gqlArgs, '{ name order }', targets, 'order');
};
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'order name',
expected,
sortKey: 'order',
});

test(
`No 'where' argument`,
Expand Down
11 changes: 8 additions & 3 deletions packages/fields/src/types/Uuid/filterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ export const initItems = () => {
// See https://github.com/keystonejs/keystone-5/issues/391

export const filterTests = withKeystone => {
const match = (keystone, gqlArgs, targets) => {
return matchFilter(keystone, gqlArgs, '{ order otherId }', targets, 'order');
};
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'order otherId',
expected,
sortKey: 'order',
});

test(
`No 'where' argument`,
Expand Down
11 changes: 8 additions & 3 deletions packages/fields/tests/idFilterTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ const getIDs = async keystone => {
};

export const filterTests = withKeystone => {
const match = (keystone, filter, targets) => {
return matchFilter(keystone, filter, '{ id name }', targets, 'name');
};
const match = (keystone, queryArgs, expected) =>
matchFilter({
keystone,
queryArgs,
fieldSelection: 'id name',
expected,
sortKey: 'name',
});

test(
'No filter',
Expand Down
15 changes: 9 additions & 6 deletions packages/test-utils/lib/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,15 @@ const sorted = (arr, keyFn) => {
return arr;
};

const matchFilter = (keystone, gqlArgs, fields, target, sortkey) => {
gqlArgs = gqlArgs ? `(${gqlArgs})` : '';
const snippet = `allTests ${gqlArgs} ${fields}`;
return graphqlRequest({ keystone, query: `query { ${snippet} }` }).then(({ data }) => {
const value = sortkey ? sorted(data.allTests || [], i => i[sortkey]) : data.allTests;
expect(value).toEqual(target);
const matchFilter = ({ keystone, queryArgs, fieldSelection, expected, sortKey }) => {
return graphqlRequest({
keystone,
query: `query {
allTests${queryArgs ? `(${queryArgs})` : ''} { ${fieldSelection} }
}`,
}).then(({ data }) => {
const value = sortKey ? sorted(data.allTests || [], i => i[sortKey]) : data.allTests;
expect(value).toEqual(expected);
});
};

Expand Down