Skip to content

Commit

Permalink
Add tests for unsupported field types (#4061)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Oct 28, 2020
1 parent b71ac00 commit 3dd5c57
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-toes-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': patch
---

Updated error handling to throw Error objects rather than Strings for invalid field configurations.
22 changes: 14 additions & 8 deletions packages/keystone/lib/ListTypes/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ module.exports = class List {
// Add an 'id' field if none supplied
if (!sanitisedFieldsConfig.id) {
if (typeof this.adapter.parentAdapter.getDefaultPrimaryKeyConfig !== 'function') {
throw (
throw new Error(
`No 'id' field given for the '${this.key}' list and the list adapter ` +
`in used (${this.adapter.key}) doesn't supply a default primary key config ` +
`(no 'getDefaultPrimaryKeyConfig()' function)`
`in used (${this.adapter.key}) doesn't supply a default primary key config ` +
`(no 'getDefaultPrimaryKeyConfig()' function)`
);
}
// Rebuild the object so id is "first"
Expand All @@ -184,23 +184,29 @@ module.exports = class List {
// Helpful errors for misconfigured lists
Object.entries(sanitisedFieldsConfig).forEach(([fieldKey, fieldConfig]) => {
if (!this.isAuxList && fieldKey[0] === '_') {
throw `Invalid field name "${fieldKey}". Field names cannot start with an underscore.`;
throw new Error(
`Invalid field name "${fieldKey}". Field names cannot start with an underscore.`
);
}
if (typeof fieldConfig.type === 'undefined') {
throw (
throw new Error(
`The '${this.key}.${fieldKey}' field doesn't specify a valid type. ` +
`(${this.key}.${fieldKey}.type is undefined)`
`(${this.key}.${fieldKey}.type is undefined)`
);
}
const adapters = fieldConfig.type.adapters;
if (typeof adapters === 'undefined' || Object.entries(adapters).length === 0) {
throw `The type given for the '${this.key}.${fieldKey}' field doesn't define any adapters.`;
throw new Error(
`The type given for the '${this.key}.${fieldKey}' field doesn't define any adapters.`
);
}
});

Object.values(sanitisedFieldsConfig).forEach(({ type }) => {
if (!type.adapters[this.adapterName]) {
throw `Adapter type "${this.adapterName}" does not support field type "${type.type}"`;
throw new Error(
`Adapter type "${this.adapterName}" does not support field type "${type.type}"`
);
}
});

Expand Down
51 changes: 51 additions & 0 deletions tests/api-tests/fields/unsupported.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const globby = require('globby');
const path = require('path');
const { multiAdapterRunners, setupServer } = require('@keystonejs/test-utils');

const testModules = globby.sync(`packages/**/src/**/test-fixtures.js`, {
absolute: true,
});
testModules.push(path.resolve('packages/fields/tests/test-fixtures.js'));

multiAdapterRunners().map(({ adapterName, after }) => {
const unsupportedModules = testModules
.map(require)
.filter(({ unSupportedAdapterList = [] }) => unSupportedAdapterList.includes(adapterName));
if (unsupportedModules.length > 0) {
describe(`${adapterName} adapter`, () => {
unsupportedModules.forEach(mod => {
(mod.testMatrix || ['default']).forEach(matrixValue => {
const listKey = 'Test';

describe(`${mod.name} - Unsupported field type`, () => {
beforeAll(() => {
if (mod.beforeAll) {
mod.beforeAll();
}
});
afterAll(async () => {
if (mod.afterAll) {
await mod.afterAll();
}
// We expect setup to fail, so disconnect can be a noop
await after({ disconnect: () => {} });
});

test('Delete', async () => {
const createLists = keystone => {
// Create a list with all the fields required for testing
keystone.createList(listKey, { fields: mod.getTestFields(matrixValue) });
};
await expect(async () => setupServer({ adapterName, createLists })).rejects.toThrow(
Error
);
});
});
});
});
});
} else {
// Appease jest, which doesn't like it when you have an empty test file.
test('noop', () => {});
}
});

0 comments on commit 3dd5c57

Please sign in to comment.