Skip to content

Commit

Permalink
Merge branch 'master' into slug-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Aug 12, 2020
2 parents a70fa2b + 7422922 commit 8155fb0
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-bananas-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/fields': patch
---

Added `Filter` tests for the `Url` field type
282 changes: 282 additions & 0 deletions packages/fields/src/types/Url/test-fixtures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,289 @@
import { matchFilter } from '@keystonejs/test-utils';
import Text from './';
import Url from './';

export const name = 'Url';
export { Url as type };
export const exampleValue = '"https://keystonejs.org"';
export const exampleValue2 = '"https://thinkmill.com.au"';
export const supportsUnique = true;

export const getTestFields = () => {
return {
order: { type: Text },
name: { type: Url },
};
};

export const initItems = () => {
return [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
{ order: 'f', name: null },
{ order: 'g' },
];
};

// JM: These tests are Mongo/Mongoose specific due to null handling (filtering and ordering)
// See https://github.com/keystonejs/keystone/issues/391

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

test(
`No 'where' argument`,
withKeystone(({ keystone }) =>
match(keystone, '', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);
test(
`Empty 'where' argument'`,
withKeystone(({ keystone }) =>
match(keystone, '', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);

test(
`Filter: {key} (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name: "https://fooBAR.com" }', [
{ order: 'd', name: 'https://fooBAR.com' },
])
)
);
test(
`Filter: {key}_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_i: "https://fooBAR.com" }', [
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
])
)
);

test(
`Filter: {key}_not (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not: "https://fooBAR.com" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);
test(
`Filter: {key}_not_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_i: "https://fooBAR.com" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);

test(
`Filter: {key}_contains (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_contains: "oo" }', [
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
])
)
);
test(
`Filter: {key}_contains_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_contains_i: "oo" }', [
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
])
)
);

test(
`Filter: {key}_not_contains (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_contains: "oo" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);
test(
`Filter: {key}_not_contains_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_contains_i: "oo" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);

test(
`Filter: {key}_starts_with (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_starts_with: "https://foo" }', [
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
])
)
);
test(
`Filter: {key}_starts_with_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_starts_with_i: "https://foo" }', [
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
])
)
);

test(
`Filter: {key}_not_starts_with (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_starts_with: "https://foo" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);

test(
`Filter: {key}_not_starts_with_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_starts_with_i: "https://foo" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);

test(
`Filter: {key}_ends_with (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_ends_with: "BAR.com" }', [
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
])
)
);
test(
`Filter: {key}_ends_with_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_ends_with_i: "BAR.com" }', [
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
])
)
);

test(
`Filter: {key}_not_ends_with (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_ends_with: "BAR.com" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'e', name: 'https://foobar.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);
test(
`Filter: {key}_not_ends_with_i (case-insensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_ends_with_i: "BAR.com" }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);

test(
`Filter: {key}_in (case-sensitive, empty list)`,
withKeystone(({ keystone }) => match(keystone, 'where: { name_in: [] }', []))
);
test(
`Filter: {key}_in (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_in: ["", "https://FOOBAR.com"] }', [
{ order: 'a', name: '' },
{ order: 'c', name: 'https://FOOBAR.com' },
])
)
);

test(
`Filter: {key}_not_in (case-sensitive, empty list)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_in: [] }', [
{ order: 'a', name: '' },
{ order: 'b', name: 'https://other.com' },
{ order: 'c', name: 'https://FOOBAR.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);
test(
`Filter: {key}_not_in (case-sensitive)`,
withKeystone(({ keystone }) =>
match(keystone, 'where: { name_not_in: ["", "https://FOOBAR.com"] }', [
{ order: 'b', name: 'https://other.com' },
{ order: 'd', name: 'https://fooBAR.com' },
{ order: 'e', name: 'https://foobar.com' },
{ order: 'f', name: null },
{ order: 'g', name: null },
])
)
);
};

0 comments on commit 8155fb0

Please sign in to comment.