diff --git a/frontend/models/Collection.test.js b/frontend/models/Collection.test.js index a9a1857d6..ca7868243 100644 --- a/frontend/models/Collection.test.js +++ b/frontend/models/Collection.test.js @@ -1,10 +1,6 @@ /* eslint-disable */ import Collection from './Collection'; - -jest.mock('utils/ApiClient', () => ({ - client: { post: {} }, -})); -jest.mock('stores', () => ({ errors: {} })); +const { client } = require('utils/ApiClient'); describe('Collection model', () => { test('should initialize with data', () => { @@ -15,40 +11,34 @@ describe('Collection model', () => { expect(collection.name).toBe('Engineering'); }); - describe('#update', () => { - test('should update', async () => { + describe('#fetch', () => { + test('should update data', async () => { + client.post = jest.fn(() => ({ + data: { + name: 'New collection', + }, + })) + const collection = new Collection({ id: 123, name: 'Engineering', }); - collection.client = { - post: jest.fn(() => ({ - data: { - name: 'New collection', - }, - })), - }; - await collection.update(); - - expect(collection.client.post).toHaveBeenCalledWith('/collections.info', { - id: 123, - }); + await collection.fetch(); expect(collection.name).toBe('New collection'); }); test('should report errors', async () => { + client.post = jest.fn(() => Promise.reject()) + const collection = new Collection({ id: 123, }); - collection.client = { - post: jest.fn(() => Promise.reject), - }; collection.errors = { add: jest.fn(), }; - await collection.update(); + await collection.fetch(); expect(collection.errors.add).toHaveBeenCalledWith( 'Collection failed loading' diff --git a/frontend/utils/__mocks__/ApiClient.js b/frontend/utils/__mocks__/ApiClient.js new file mode 100644 index 000000000..d53d1d2dc --- /dev/null +++ b/frontend/utils/__mocks__/ApiClient.js @@ -0,0 +1,5 @@ +export default { + client: { + post: jest.fn(() => Promise.resolve), + }, +}; diff --git a/frontend/utils/setupJest.js b/frontend/utils/setupJest.js index 61b717c2f..62af3f25d 100644 --- a/frontend/utils/setupJest.js +++ b/frontend/utils/setupJest.js @@ -9,5 +9,6 @@ const snap = children => { expect(toJson(wrapper)).toMatchSnapshot(); }; +global.fetch = require('jest-fetch-mock'); global.localStorage = localStorage; global.snap = snap; diff --git a/package.json b/package.json index ba8d2ae72..cf1677423 100644 --- a/package.json +++ b/package.json @@ -180,6 +180,7 @@ "flow-bin": "^0.45.0", "identity-obj-proxy": "^3.0.0", "jest-cli": "^20.0.0", + "jest-fetch-mock": "^1.2.0", "koa-webpack-dev-middleware": "1.4.5", "koa-webpack-hot-middleware": "1.0.3", "lint-staged": "^3.4.0", diff --git a/yarn.lock b/yarn.lock index 3d3a545a9..d7fe0cb8a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4543,6 +4543,12 @@ jest-environment-node@^20.0.3: jest-mock "^20.0.3" jest-util "^20.0.3" +jest-fetch-mock@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-1.2.0.tgz#15ac5c2d32d2c888bd3d132b682a4deb17f3ab35" + dependencies: + whatwg-fetch "1.0.0" + jest-haste-map@^20.0.3: version "20.0.3" resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-20.0.3.tgz#6377d537eaf34eb5f75121a691cae3fde82ba971" @@ -6981,19 +6987,13 @@ promise@7.x, promise@^7.0.3, promise@^7.1.1: dependencies: asap "~2.0.3" -prop-types@^15.5.10: +prop-types@^15.5.10, prop-types@^15.5.4, prop-types@^15.5.8: version "15.5.10" resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.10.tgz#2797dfc3126182e3a95e3dfbb2e893ddd7456154" dependencies: fbjs "^0.8.9" loose-envify "^1.3.1" -prop-types@^15.5.4, prop-types@^15.5.8: - version "15.5.8" - resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.8.tgz#6b7b2e141083be38c8595aa51fc55775c7199394" - dependencies: - fbjs "^0.8.9" - proto-list@~1.2.1: version "1.2.4" resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849" @@ -8923,7 +8923,7 @@ whatwg-encoding@^1.0.1: dependencies: iconv-lite "0.4.13" -whatwg-fetch@>=0.10.0: +whatwg-fetch@1.0.0, whatwg-fetch@>=0.10.0: version "1.0.0" resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-1.0.0.tgz#01c2ac4df40e236aaa18480e3be74bd5c8eb798e"