Skip to content

Commit

Permalink
Move test-fixtures into the new field packages (#5212)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 24, 2021
1 parent 9479620 commit 76e5c7b
Show file tree
Hide file tree
Showing 20 changed files with 114 additions and 73 deletions.
7 changes: 7 additions & 0 deletions .changeset/calm-bags-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@keystone-next/fields': patch
'@keystone-next/fields-document': patch
'@keystone-next/api-tests-legacy': patch
---

Moved test fixtures into the new packages.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"!packages/**/dist/**",
"!packages/core/tests/default-entry/index.js",
"!packages/fields/**/test-fixtures.js",
"!packages/fields/**/test-fixtures.ts",
"!packages/fields/types.js"
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-ignore
import { Text } from '@keystone-next/fields-legacy';
import { DocumentFieldType } from './base-field-type';
import { DocumentFieldType } from '../base-field-type';

export const name = 'Document';
export const type = DocumentFieldType;
Expand All @@ -12,11 +13,11 @@ export const supportsUnique = false;
export const fieldName = 'content';
export const subfieldName = 'document';

export const fieldConfig = () => ({ ___validateAndNormalize: x => x });
export const fieldConfig = () => ({ ___validateAndNormalize: (x: any) => x });

export const getTestFields = () => ({
name: { type: Text },
content: { type, ___validateAndNormalize: x => x },
content: { type, ___validateAndNormalize: (x: any) => x },
});

export const initItems = () => {
Expand Down
2 changes: 2 additions & 0 deletions packages-next/fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"main": "dist/fields.cjs.js",
"module": "dist/fields.esm.js",
"devDependencies": {
"@keystone-next/server-side-graphql-client-legacy": "2.0.1",
"@keystone-next/test-utils-legacy": "14.0.1",
"typescript": "^4.2.3"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// @ts-ignore
import { getItems } from '@keystone-next/server-side-graphql-client-legacy';
// @ts-ignore
import { Text } from '@keystone-next/fields-legacy';
import { AutoIncrement } from './index';
// @ts-ignore
import { AutoIncrement } from '@keystone-next/fields-auto-increment-legacy/src';

type MatrixValue = typeof testMatrix[number];

export const name = 'AutoIncrement';
export const type = AutoIncrement;
export const testMatrix = ['ID', 'Int'];
export const exampleValue = matrixValue => (matrixValue === 'ID' ? '35' : 35);
export const exampleValue2 = matrixValue => (matrixValue === 'ID' ? '36' : 36);
export const testMatrix = ['ID', 'Int'] as const;
export const exampleValue = (matrixValue: MatrixValue) => (matrixValue === 'ID' ? '35' : 35);
export const exampleValue2 = (matrixValue: MatrixValue) => (matrixValue === 'ID' ? '36' : 36);
export const supportsUnique = true;
export const fieldName = 'orderNumber';
export const skipCreateTest = false;
Expand All @@ -16,12 +21,12 @@ export const skipUpdateTest = true;
export const unSupportedAdapterList = ['mongoose', 'prisma_sqlite'];

// Be default, `AutoIncrement` are read-only. But for `isRequired` test purpose, we need to bypass these restrictions.
export const fieldConfig = matrixValue => ({
export const fieldConfig = (matrixValue: MatrixValue) => ({
gqlType: matrixValue,
access: { create: true, update: true },
});

export const getTestFields = matrixValue => ({
export const getTestFields = (matrixValue: MatrixValue) => ({
name: { type: Text },
orderNumber: { type, gqlType: matrixValue, access: { create: true } },
});
Expand All @@ -38,7 +43,7 @@ export const initItems = () => {
];
};

export const storedValues = matrixValue =>
export const storedValues = (matrixValue: MatrixValue) =>
matrixValue === 'ID'
? [
{ name: 'product1', orderNumber: '1' },
Expand All @@ -61,10 +66,10 @@ export const storedValues = matrixValue =>

export const supportedFilters = () => [];

export const filterTests = (withKeystone, matrixValue) => {
export const filterTests = (withKeystone: (arg: any) => any, matrixValue: MatrixValue) => {
const _storedValues = storedValues(matrixValue);
const _f = matrixValue === 'ID' ? x => x.toString() : x => x;
const match = async (keystone, where, expected) =>
const _f = matrixValue === 'ID' ? (x: any) => x.toString() : (x: any) => x;
const match = async (keystone: any, where: Record<string, any>, expected: any[]) =>
expect(
await getItems({
keystone,
Expand All @@ -77,75 +82,87 @@ export const filterTests = (withKeystone, matrixValue) => {

test(
'Filter: orderNumber',
withKeystone(({ keystone }) => match(keystone, { orderNumber: _f(1) }, [0]))
withKeystone(({ keystone }: { keystone: any }) => match(keystone, { orderNumber: _f(1) }, [0]))
);

test(
'Filter: orderNumber_not',
withKeystone(({ keystone }) => match(keystone, { orderNumber_not: _f(1) }, [1, 2, 3, 4, 5, 6]))
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_not: _f(1) }, [1, 2, 3, 4, 5, 6])
)
);

test(
'Filter: orderNumber_not null',
withKeystone(({ keystone }) =>
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_not: null }, [0, 1, 2, 3, 4, 5, 6])
)
);

test(
'Filter: orderNumber_lt',
withKeystone(({ keystone }) => match(keystone, { orderNumber_lt: _f(2) }, [0]))
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_lt: _f(2) }, [0])
)
);

test(
'Filter: orderNumber_lte',
withKeystone(({ keystone }) => match(keystone, { orderNumber_lte: _f(2) }, [0, 1]))
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_lte: _f(2) }, [0, 1])
)
);

test(
'Filter: orderNumber_gt',
withKeystone(({ keystone }) => match(keystone, { orderNumber_gt: _f(2) }, [2, 3, 4, 5, 6]))
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_gt: _f(2) }, [2, 3, 4, 5, 6])
)
);

test(
'Filter: orderNumber_gte',
withKeystone(({ keystone }) => match(keystone, { orderNumber_gte: _f(2) }, [1, 2, 3, 4, 5, 6]))
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_gte: _f(2) }, [1, 2, 3, 4, 5, 6])
)
);

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

test(
'Filter: orderNumber_not_in (empty list)',
withKeystone(({ keystone }) =>
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_not_in: [] }, [0, 1, 2, 3, 4, 5, 6])
)
);

test(
'Filter: orderNumber_in',
withKeystone(({ keystone }) =>
match(keystone, { orderNumber_in: [1, 2, 3].map(_f) }, [0, 1, 2])
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_in: ([1, 2, 3] as const).map(_f) }, [0, 1, 2])
)
);

test(
'Filter: orderNumber_not_in',
withKeystone(({ keystone }) =>
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_not_in: [1, 2, 3].map(_f) }, [3, 4, 5, 6])
)
);

test(
'Filter: orderNumber_in null',
withKeystone(({ keystone }) => match(keystone, { orderNumber_in: [null] }, []))
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_in: [null] }, [])
)
);

test(
'Filter: orderNumber_not_in null',
withKeystone(({ keystone }) =>
withKeystone(({ keystone }: { keystone: any }) =>
match(keystone, { orderNumber_not_in: [null] }, [0, 1, 2, 3, 4, 5, 6])
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Text from '../Text';
import Checkbox from './';
// @ts-ignore
import { Checkbox, Text } from '@keystone-next/fields-legacy';

export const name = 'Checkbox';
export const type = Checkbox;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Text from '../Text';
import Decimal from './';
// @ts-ignore
import { Text, Decimal } from '@keystone-next/fields-legacy';
import { AdapterName } from '@keystone-next/test-utils-legacy';

export const name = 'Decimal';
export const type = Decimal;
Expand Down Expand Up @@ -36,7 +37,7 @@ export const storedValues = () => [
{ name: 'price7', price: null },
];

export const supportedFilters = adapterName => [
export const supportedFilters = (adapterName: AdapterName) => [
'null_equality',
'equality',
'ordering',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Text from '../Text';
import Float from '.';
// @ts-ignore
import { Text, Float } from '@keystone-next/fields-legacy';

export const name = 'Float';
export const type = Float;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Text from '../Text';
import Integer from './';
// @ts-ignore
import { Text, Integer } from '@keystone-next/fields-legacy';

export const name = 'Integer';
export const type = Integer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-ignore
import { Text } from '@keystone-next/fields-legacy';

import { MongoId } from './index';
// @ts-ignore
import { MongoId } from '@keystone-next/fields-mongoid-legacy';

export const name = 'MongoId';
export const type = MongoId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Text from '../Text';
import Password from './';
// @ts-ignore
import { Text, Password } from '@keystone-next/fields-legacy';

export const name = 'Password';
export const type = Password;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Text from '../Text';
import Select from './';
// @ts-ignore
import { Text, Select } from '@keystone-next/fields-legacy';

type MatrixValue = typeof testMatrix[number];

export const name = 'Select';
export const type = Select;
export const exampleValue = matrixValue =>
export const exampleValue = (matrixValue: MatrixValue) =>
matrixValue === 'enum' ? 'thinkmill' : matrixValue === 'string' ? 'a string' : 1;
export const exampleValue2 = matrixValue =>
export const exampleValue2 = (matrixValue: MatrixValue) =>
matrixValue === 'enum' ? 'atlassian' : matrixValue === 'string' ? '1number' : 2;
export const supportsUnique = true;
export const fieldConfig = matrixValue => ({
export const fieldConfig = (matrixValue: MatrixValue) => ({
dataType: matrixValue,
options:
matrixValue === 'enum'
Expand Down Expand Up @@ -41,14 +43,14 @@ export const fieldName = 'company';

export const supportedFilters = () => ['null_equality', 'equality', 'in_empty_null', 'in_equal'];

export const testMatrix = ['enum', 'string', 'integer'];
export const testMatrix = ['enum', 'string', 'integer'] as const;

export const getTestFields = matrixValue => ({
export const getTestFields = (matrixValue: MatrixValue) => ({
name: { type: Text },
company: { type, ...fieldConfig(matrixValue) },
});

export const initItems = matrixValue => {
export const initItems = (matrixValue: MatrixValue) => {
if (matrixValue === 'enum') {
return [
{ name: 'a', company: 'thinkmill' },
Expand Down Expand Up @@ -83,7 +85,7 @@ export const initItems = matrixValue => {
return [];
};

export const storedValues = matrixValue => {
export const storedValues = (matrixValue: MatrixValue) => {
if (matrixValue === 'enum') {
return [
{ name: 'a', company: 'thinkmill' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Text from './';
// @ts-ignore
import { Text } from '@keystone-next/fields-legacy';
import { AdapterName } from '@keystone-next/test-utils-legacy';

export const name = 'Text';
export const type = Text;
Expand Down Expand Up @@ -31,7 +33,7 @@ export const storedValues = () => [
{ name: 'g', testField: null },
];

export const supportedFilters = adapterName => [
export const supportedFilters = (adapterName: AdapterName) => [
'null_equality',
'equality',
adapterName !== 'prisma_sqlite' && 'equality_case_insensitive',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @ts-ignore
import { getItems } from '@keystone-next/server-side-graphql-client-legacy';
import Text from '../Text';
import DateTimeUtc from './';
// @ts-ignore
import { Text, DateTimeUtc } from '@keystone-next/fields-legacy';
import { AdapterName } from '@keystone-next/test-utils-legacy';

export const name = 'DateTimeUtc';
export const type = DateTimeUtc;
Expand Down Expand Up @@ -41,8 +43,13 @@ export const supportedFilters = () => [
'in_equal',
];

export const filterTests = withKeystone => {
const match = async (keystone, where, expected, sortBy = 'name_ASC') =>
export const filterTests = (withKeystone: (args: any) => any) => {
const match = async (
keystone: any,
where: Record<string, any> | undefined,
expected: any,
sortBy = 'name_ASC'
) =>
expect(
await getItems({
keystone,
Expand All @@ -55,7 +62,7 @@ export const filterTests = withKeystone => {

test(
'Sorting: sortBy: lastOnline_ASC',
withKeystone(({ keystone, adapterName }) =>
withKeystone(({ keystone, adapterName }: { keystone: any; adapterName: AdapterName }) =>
match(
keystone,
undefined,
Expand Down Expand Up @@ -95,7 +102,7 @@ export const filterTests = withKeystone => {

test(
'Sorting: sortBy: lastOnline_DESC',
withKeystone(({ keystone, adapterName }) =>
withKeystone(({ keystone, adapterName }: { keystone: any; adapterName: AdapterName }) =>
match(
keystone,
undefined,
Expand Down
Loading

1 comment on commit 76e5c7b

@vercel
Copy link

@vercel vercel bot commented on 76e5c7b Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.