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

Move mocks to test utils #3138

Merged
merged 6 commits into from
Jun 10, 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/strange-flies-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/keystone': patch
'@keystonejs/test-utils': minor
---

Moved adapter mocks into test utils package.
1 change: 1 addition & 0 deletions packages/keystone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"terminal-link": "^1.3.0"
},
"devDependencies": {
"@keystonejs/test-utils": "*",
"p-is-promise": "^3.0.0",
"tmp": "^0.2.1"
},
Expand Down
125 changes: 1 addition & 124 deletions packages/keystone/tests/List.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const gql = require('graphql-tag');
const { print } = require('graphql/language/printer');
const { MockAdapter, MockIdType, MockListAdapter } = require('@keystonejs/test-utils');

// We don't want to actually log, so we mock it before we require the class
jest.doMock('@keystonejs/logger', () => ({
Expand All @@ -17,130 +18,6 @@ function resolveViewPath(viewPath) {
return path.join(fieldsPackagePath, 'src', 'types', viewPath);
}

class MockFieldImplementation {
constructor() {
this.access = {
public: {
create: false,
read: true,
update: false,
delete: false,
},
};
this.config = {};
this.hooks = {};
}
getAdminMeta() {
return { path: 'id' };
}
gqlOutputFields() {
return ['id: ID'];
}
gqlQueryInputFields() {
return ['id: ID'];
}
get gqlUpdateInputFields() {
return ['id: ID'];
}
get gqlCreateInputFields() {
return ['id: ID'];
}
getGqlAuxTypes() {
return [];
}
getGqlAuxQueries() {
return [];
}
getGqlAuxMutations() {
return [];
}
gqlOutputFieldResolvers() {
return {};
}
gqlAuxQueryResolvers() {
return {};
}
gqlAuxMutationResolvers() {
return {};
}
gqlAuxFieldResolvers() {
return {};
}
extendAdminViews(views) {
return views;
}
getDefaultValue() {
return;
}
async resolveInput({ resolvedData }) {
return resolvedData.id;
}
async validateInput() {}
async beforeChange() {}
async afterChange() {}
async beforeDelete() {}
async validateDelete() {}
async afterDelete() {}
}
class MockFieldAdapter {}

const MockIdType = {
implementation: MockFieldImplementation,
views: {},
adapters: { mock: MockFieldAdapter },
};

class MockListAdapter {
name = 'mock';
constructor(parentAdapter) {
this.parentAdapter = parentAdapter;
this.index = 3;
this.items = {
0: { name: 'a', email: '[email protected]', index: 0 },
1: { name: 'b', email: '[email protected]', index: 1 },
2: { name: 'c', email: '[email protected]', index: 2 },
};
}
newFieldAdapter = () => new MockFieldAdapter();
create = async item => {
this.items[this.index] = {
...item,
index: this.index,
};
this.index += 1;
return this.items[this.index - 1];
};
findById = id => this.items[id];
delete = async id => {
this.items[id] = undefined;
};
itemsQuery = async ({ where: { id_in: ids, id, id_not_in } }, { meta = false } = {}) => {
if (meta) {
return {
count: (id !== undefined
? [this.items[id]]
: ids.filter(i => !id_not_in || !id_not_in.includes(i)).map(i => this.items[i])
).length,
};
} else {
return id !== undefined
? [this.items[id]]
: ids.filter(i => !id_not_in || !id_not_in.includes(i)).map(i => this.items[i]);
}
};
itemsQueryMeta = async args => this.itemsQuery(args, { meta: true });
update = (id, item) => {
this.items[id] = { ...this.items[id], ...item };
return this.items[id];
};
}

class MockAdapter {
name = 'mock';
newListAdapter = () => new MockListAdapter(this);
getDefaultPrimaryKeyConfig = () => ({ type: MockIdType });
}

Text.adapters['mock'] = {};
Checkbox.adapters['mock'] = {};
Float.adapters['mock'] = {};
Expand Down
6 changes: 6 additions & 0 deletions packages/test-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const {
authedGraphqlRequest,
networkedGraphqlRequest,
matchFilter,
MockAdapter,
MockListAdapter,
MockIdType,
} = require('./lib/test-utils');

module.exports = {
Expand All @@ -14,4 +17,7 @@ module.exports = {
authedGraphqlRequest,
networkedGraphqlRequest,
matchFilter,
MockAdapter,
MockListAdapter,
MockIdType,
};
127 changes: 127 additions & 0 deletions packages/test-utils/lib/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,138 @@ const matchFilter = ({ keystone, queryArgs, fieldSelection, expected, sortKey })
});
};

class MockFieldImplementation {
constructor() {
this.access = {
public: {
create: false,
read: true,
update: false,
delete: false,
},
};
this.config = {};
this.hooks = {};
}
getAdminMeta() {
return { path: 'id' };
}
gqlOutputFields() {
return ['id: ID'];
}
gqlQueryInputFields() {
return ['id: ID'];
}
get gqlUpdateInputFields() {
return ['id: ID'];
}
get gqlCreateInputFields() {
return ['id: ID'];
}
getGqlAuxTypes() {
return [];
}
getGqlAuxQueries() {
return [];
}
getGqlAuxMutations() {
return [];
}
gqlOutputFieldResolvers() {
return {};
}
gqlAuxQueryResolvers() {
return {};
}
gqlAuxMutationResolvers() {
return {};
}
gqlAuxFieldResolvers() {
return {};
}
extendAdminViews(views) {
return views;
}
getDefaultValue() {
return;
}
async resolveInput({ resolvedData }) {
return resolvedData.id;
}
async validateInput() {}
async beforeChange() {}
async afterChange() {}
async beforeDelete() {}
async validateDelete() {}
async afterDelete() {}
}
class MockFieldAdapter {}

const MockIdType = {
implementation: MockFieldImplementation,
views: {},
adapters: { mock: MockFieldAdapter },
};

class MockListAdapter {
name = 'mock';
constructor(parentAdapter) {
this.parentAdapter = parentAdapter;
this.index = 3;
this.items = {
0: { name: 'a', email: '[email protected]', index: 0 },
1: { name: 'b', email: '[email protected]', index: 1 },
2: { name: 'c', email: '[email protected]', index: 2 },
};
}
newFieldAdapter = () => new MockFieldAdapter();
create = async item => {
this.items[this.index] = {
...item,
index: this.index,
};
this.index += 1;
return this.items[this.index - 1];
};
findById = id => this.items[id];
delete = async id => {
this.items[id] = undefined;
};
itemsQuery = async ({ where: { id_in: ids, id, id_not_in } }, { meta = false } = {}) => {
if (meta) {
return {
count: (id !== undefined
? [this.items[id]]
: ids.filter(i => !id_not_in || !id_not_in.includes(i)).map(i => this.items[i])
).length,
};
} else {
return id !== undefined
? [this.items[id]]
: ids.filter(i => !id_not_in || !id_not_in.includes(i)).map(i => this.items[i]);
}
};
itemsQueryMeta = async args => this.itemsQuery(args, { meta: true });
update = (id, item) => {
this.items[id] = { ...this.items[id], ...item };
return this.items[id];
};
}

class MockAdapter {
name = 'mock';
newListAdapter = () => new MockListAdapter(this);
getDefaultPrimaryKeyConfig = () => ({ type: MockIdType });
}

module.exports = {
setupServer,
multiAdapterRunners,
graphqlRequest,
authedGraphqlRequest,
networkedGraphqlRequest,
matchFilter,
MockAdapter,
MockListAdapter,
MockIdType,
};