Collection model
This commit is contained in:
50
frontend/models/Collection.js
Normal file
50
frontend/models/Collection.js
Normal file
@@ -0,0 +1,50 @@
|
||||
// @flow
|
||||
import { extendObservable, action, computed, runInAction } from 'mobx';
|
||||
import invariant from 'invariant';
|
||||
import _ from 'lodash';
|
||||
|
||||
import ApiClient, { client } from 'utils/ApiClient';
|
||||
import stores from 'stores';
|
||||
import ErrorsStore from 'stores/ErrorsStore';
|
||||
import type { NavigationNode } from 'types';
|
||||
|
||||
class Collection {
|
||||
createdAt: string;
|
||||
description: ?string;
|
||||
id: string;
|
||||
name: string;
|
||||
type: 'atlas' | 'journal';
|
||||
navigationTree: NavigationNode;
|
||||
updatedAt: string;
|
||||
url: string;
|
||||
|
||||
client: ApiClient;
|
||||
errors: ErrorsStore;
|
||||
|
||||
/* Actions */
|
||||
|
||||
@action update = async () => {
|
||||
try {
|
||||
const res = await this.client.post('/collections.info', { id: this.id });
|
||||
invariant(res && res.data, 'API response should be available');
|
||||
const { data } = res;
|
||||
runInAction('Collection#update', () => {
|
||||
this.updateData(data);
|
||||
});
|
||||
} catch (e) {
|
||||
this.errors.add('Collection failed loading');
|
||||
}
|
||||
};
|
||||
|
||||
updateData(data: Collection) {
|
||||
extendObservable(this, data);
|
||||
}
|
||||
|
||||
constructor(collection: Collection) {
|
||||
this.updateData(collection);
|
||||
this.client = client;
|
||||
this.errors = stores.errors;
|
||||
}
|
||||
}
|
||||
|
||||
export default Collection;
|
||||
58
frontend/models/Collection.test.js
Normal file
58
frontend/models/Collection.test.js
Normal file
@@ -0,0 +1,58 @@
|
||||
/* eslint-disable */
|
||||
import Collection from './Collection';
|
||||
|
||||
jest.mock('utils/ApiClient', () => ({
|
||||
client: { post: {} },
|
||||
}));
|
||||
jest.mock('stores', () => ({ errors: {} }));
|
||||
|
||||
describe('Collection model', () => {
|
||||
test('should initialize with data', () => {
|
||||
const collection = new Collection({
|
||||
id: 123,
|
||||
name: 'Engineering',
|
||||
});
|
||||
expect(collection.name).toBe('Engineering');
|
||||
});
|
||||
|
||||
describe('#update', () => {
|
||||
test('should update', async () => {
|
||||
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,
|
||||
});
|
||||
expect(collection.name).toBe('New collection');
|
||||
});
|
||||
|
||||
test('should report errors', async () => {
|
||||
const collection = new Collection({
|
||||
id: 123,
|
||||
});
|
||||
collection.client = {
|
||||
post: jest.fn(() => Promise.reject),
|
||||
};
|
||||
collection.errors = {
|
||||
add: jest.fn(),
|
||||
};
|
||||
|
||||
await collection.update();
|
||||
|
||||
expect(collection.errors.add).toHaveBeenCalledWith(
|
||||
'Collection failed loading'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user