Skip to content

Commit

Permalink
Don't save list in Field controllers (#2895)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz authored May 5, 2020
1 parent 547fd83 commit 72e0a4e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .changeset/chatty-garlics-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/app-admin-ui': minor
'@keystonejs/fields': minor
---

The base `FieldController` class no longer takes the owning list as a second argument.
2 changes: 1 addition & 1 deletion packages/app-admin-ui/client/classes/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class List {

this.fields = fields.map(fieldConfig => {
const [Controller] = adminMeta.readViews([views[fieldConfig.path].Controller]);
return new Controller(fieldConfig, this, adminMeta, views[fieldConfig.path]);
return new Controller(fieldConfig, adminMeta, views[fieldConfig.path]);
});

this._fieldsByPath = arrayToObject(this.fields, 'path');
Expand Down
3 changes: 1 addition & 2 deletions packages/fields/src/Controller.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import isEqual from 'lodash.isequal';

export default class FieldController {
constructor(config, list, adminMeta, views) {
constructor(config, adminMeta, views) {
this.config = config;
this.label = config.label;
this.path = config.path;
this.type = config.type;
this.maybeAccess = config.access;
this.isPrimaryKey = config.isPrimaryKey;
this.list = list;
this.adminMeta = adminMeta;
this.views = views;

Expand Down
32 changes: 11 additions & 21 deletions packages/fields/tests/Controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,81 +10,72 @@ const config = {

describe('new Controller()', () => {
test('new Controller() - Smoke test', () => {
const controller = new FieldController(config, 'list', 'adminMeta');
const controller = new FieldController(config, 'adminMeta');
expect(controller).not.toBeNull();

expect(controller.config).toEqual(config);
expect(controller.label).toEqual('label');
expect(controller.type).toEqual('type');
expect(controller.list).toEqual('list');
expect(controller.adminMeta).toEqual('adminMeta');
});
});

test('getQueryFragment()', () => {
const controller = new FieldController(config, 'list', 'adminMeta');
const controller = new FieldController(config, 'adminMeta');

const value = controller.getQueryFragment();
expect(value).toEqual('path');
});

describe('serialize()', () => {
test('serialize() - path exists', () => {
const controller = new FieldController(config, 'list', 'adminMeta');
const controller = new FieldController(config, 'adminMeta');
let value = controller.serialize({ path: 'some_value' });
expect(value).toEqual('some_value');
});

test('serialize() - path does not exist', () => {
const controller = new FieldController(config, 'list', 'adminMeta');
const controller = new FieldController(config, 'adminMeta');
const value = controller.serialize({});
expect(value).toEqual(null);
});
});

describe('deserialize()', () => {
test('deserialize() - path exists', () => {
const controller = new FieldController(config, 'list', 'adminMeta');
const controller = new FieldController(config, 'adminMeta');
let value = controller.deserialize({ path: 'some_value' });
expect(value).toEqual('some_value');
});

test('deserialize() - path does not exist', () => {
const controller = new FieldController(config, 'list', 'adminMeta');
const controller = new FieldController(config, 'adminMeta');
const value = controller.deserialize({});
expect(value).toEqual(undefined);
});
});

describe('getDefaultValue()', () => {
test('No default', () => {
const controller = new FieldController(config, 'list', 'adminMeta');
const controller = new FieldController(config, 'adminMeta');
const value = controller.getDefaultValue({});
expect(value).toEqual(undefined);
});

test('Default defined as `undefined`', () => {
const controller = new FieldController(
{ ...config, defaultValue: undefined },
'list',
'adminMeta'
);
const controller = new FieldController({ ...config, defaultValue: undefined }, 'adminMeta');
const value = controller.getDefaultValue({});
expect(value).toEqual(undefined);
});

test('Default defined as `null`', () => {
const controller = new FieldController({ ...config, defaultValue: null }, 'list', 'adminMeta');
const controller = new FieldController({ ...config, defaultValue: null }, 'adminMeta');
const value = controller.getDefaultValue({});
expect(value).toEqual(null);
});

test('Default defined as a string', () => {
const controller = new FieldController(
{ ...config, defaultValue: 'default' },
'list',
'adminMeta'
);
const controller = new FieldController({ ...config, defaultValue: 'default' }, 'adminMeta');
const value = controller.getDefaultValue({});
expect(value).toEqual('default');
});
Expand All @@ -93,7 +84,6 @@ describe('getDefaultValue()', () => {
test('function is executed', () => {
const controller = new FieldController(
{ ...config, defaultValue: () => 'default' },
'list',
'adminMeta'
);
const value = controller.getDefaultValue({});
Expand All @@ -104,7 +94,7 @@ describe('getDefaultValue()', () => {
const originalInput = {};
const prefill = {};
const defaultValue = jest.fn(() => 'default');
const controller = new FieldController({ ...config, defaultValue }, 'list', 'adminMeta');
const controller = new FieldController({ ...config, defaultValue }, 'adminMeta');
controller.getDefaultValue({ originalInput, prefill });
expect(defaultValue).toHaveBeenCalledWith({ originalInput, prefill });
});
Expand Down

0 comments on commit 72e0a4e

Please sign in to comment.