Skip to content

Commit

Permalink
Add filter & CRUD tests for COLOR field
Browse files Browse the repository at this point in the history
  • Loading branch information
singhArmani committed Aug 17, 2020
1 parent 63c1ff4 commit 97ce44d
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/shiny-countries-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/api-tests': patch
'@keystonejs/fields-color': patch
---

Updated filter and CRUD tests for `Color` field type.
2 changes: 1 addition & 1 deletion api-tests/fields.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { multiAdapterRunners, setupServer } = require('@keystonejs/test-utils');
const { createItems } = require('@keystonejs/server-side-graphql-client');

describe('Fields', () => {
const testModules = globby.sync(`packages/fields/src/types/**/test-fixtures.js`, {
const testModules = globby.sync(`packages/**/src/**/test-fixtures.js`, {
absolute: true,
});
testModules.push(path.resolve('packages/fields/tests/test-fixtures.js'));
Expand Down
1 change: 1 addition & 0 deletions packages/fields-color/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"react": "^16.13.1"
},
"devDependencies": {
"@keystonejs/server-side-graphql-client": "*",
"react": "^16.13.1"
},
"main": "dist/fields-color.cjs.js",
Expand Down
315 changes: 315 additions & 0 deletions packages/fields-color/src/test-fixtures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,322 @@
import { createItem, getItem, getItems, updateItem } from '@keystonejs/server-side-graphql-client';
import { Color } from '.';
import { Text } from '@keystonejs/fields';

export const name = 'Color';
export { Color as type };
export const exampleValue = '"red"';
export const exampleValue2 = '"green"';
export const supportsUnique = true;

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

export const initItems = () => {
return [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
];
};

export const filterTests = withKeystone => {
const match = async (keystone, where, expected, sortBy = 'name_ASC') =>
expect(
await getItems({
keystone,
listKey: 'test',
where,
returnFields: 'name hexColor',
sortBy,
})
).toEqual(expected);

test(
'No filter',
withKeystone(({ keystone }) =>
match(keystone, undefined, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
])
)
);

test(
'Empty filter',
withKeystone(({ keystone }) =>
match(keystone, {}, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
])
)
);

test(
'Filter: hexColor',
withKeystone(({ keystone }) =>
match(keystone, { hexColor: 'rgba(45, 228, 39, 1)' }, [
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
])
)
);

test(
'Filter: hexColor_not',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_not: 'rgba(45, 228, 39, 1)' }, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
])
)
);

test(
'Filter: hexColor_not null',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_not: null }, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
])
)
);

test(
'Filter: hexColor_in (empty list)',
withKeystone(({ keystone }) => match(keystone, { hexColor_in: [] }, []))
);

test(
'Filter: hexColor_not_in (empty list)',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_not_in: [] }, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
])
)
);

test(
'Filter: hexColor_in',
withKeystone(({ keystone }) =>
match(
keystone,
{
hexColor_in: ['rgba(223, 57, 57, 1)', 'rgba(45, 228, 39, 1)'],
},
[
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
]
)
)
);

test(
'Filter: hexColor_not_in',
withKeystone(({ keystone }) =>
match(
keystone,
{
hexColor_not_in: [null, 'rgba(223, 57, 57, 1)', 'rgba(45, 228, 39, 1)'],
},
[{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' }]
)
)
);

test(
'Filter: hexColor_in null',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_in: [null] }, [{ name: 'd', hexColor: null }])
)
);

test(
'Filter: hexColor_not_in null',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_not_in: [null] }, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
])
)
);

test(
'Filter: hexColor_contains',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_contains: '57' }, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
])
)
);

test(
'Filter: hexColor_not_contains',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_not_contains: '57' }, [
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
])
)
);

test(
'Filter: hexColor_starts_with',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_starts_with: 'rgba(45' }, [
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
])
)
);
test(
'Filter: hexColor_not_starts_with',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_not_starts_with: 'rgba(45' }, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
])
)
);

test(
'Filter: hexColor_ends_with',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_ends_with: '39, 1)' }, [
{ name: 'b', hexColor: 'rgba(45, 228, 39, 1)' },
])
)
);

test(
'Filter: hexColor_not_ends_with',
withKeystone(({ keystone }) =>
match(keystone, { hexColor_not_ends_with: '39, 1)' }, [
{ name: 'a', hexColor: 'rgba(223, 57, 57, 1)' },
{ name: 'c', hexColor: 'rgba(50, 121, 206, 1)' },
{ name: 'd', hexColor: null },
])
)
);
};

export const crudTests = withKeystone => {
const withHelpers = wrappedFn => {
return async ({ keystone, listKey }) => {
const items = await getItems({
keystone,
listKey,
returnFields: 'id hexColor',
});
return wrappedFn({ keystone, listKey, items });
};
};

test(
'Create',
withKeystone(
withHelpers(async ({ keystone, listKey }) => {
const data = await createItem({
keystone,
listKey,
item: { name: 'purple', hexColor: 'rgba(154, 18, 179, 1)' },
returnFields: 'hexColor',
});
expect(data).not.toBe(null);
expect(data.hexColor).toBe('rgba(154, 18, 179, 1)');
})
)
);

test(
'Read',
withKeystone(
withHelpers(async ({ keystone, listKey, items }) => {
const data = await getItem({
keystone,
listKey,
itemId: items[0].id,
returnFields: 'hexColor',
});
console.log({ data });
expect(data).not.toBe(null);
expect(data.hexColor).toBe(items[0].hexColor);
})
)
);

describe('Update', () => {
test(
'Updating the value',
withKeystone(
withHelpers(async ({ keystone, items, listKey }) => {
const data = await updateItem({
keystone,
listKey,
item: {
id: items[0].id,
data: { hexColor: 'rgba(145, 61, 136, 1)' },
},
returnFields: 'hexColor',
});
expect(data).not.toBe(null);
expect(data.hexColor).toBe('rgba(145, 61, 136, 1)');
})
)
);

test(
'Updating the value to null',
withKeystone(
withHelpers(async ({ keystone, items, listKey }) => {
const data = await updateItem({
keystone,
listKey,
item: {
id: items[0].id,
data: { hexColor: null },
},
returnFields: 'hexColor',
});
expect(data).not.toBe(null);
expect(data.hexColor).toBe(null);
})
)
);

test(
'Updating without this field',
withKeystone(
withHelpers(async ({ keystone, items, listKey }) => {
const data = await updateItem({
keystone,
listKey,
item: {
id: items[0].id,
data: { name: 'Plum' },
},
returnFields: 'name hexColor',
});
expect(data).not.toBe(null);
expect(data.name).toBe('Plum');
expect(data.hexColor).toBe(items[0].hexColor);
})
)
);
});
};

0 comments on commit 97ce44d

Please sign in to comment.