mirror of
https://github.com/outline/outline.git
synced 2025-03-15 19:18:00 +00:00
DocumentsStore WIP
This commit is contained in:
__mocks__
frontend
20
__mocks__/localStorage.js
Normal file
20
__mocks__/localStorage.js
Normal file
@ -0,0 +1,20 @@
|
||||
const storage = {};
|
||||
|
||||
export default {
|
||||
setItem: function(key, value) {
|
||||
storage[key] = value || '';
|
||||
},
|
||||
getItem: function(key) {
|
||||
return key in storage ? storage[key] : null;
|
||||
},
|
||||
removeItem: function(key) {
|
||||
delete storage[key];
|
||||
},
|
||||
get length() {
|
||||
return Object.keys(storage).length;
|
||||
},
|
||||
key: function(i) {
|
||||
var keys = Object.keys(storage);
|
||||
return keys[i] || null;
|
||||
},
|
||||
};
|
@ -2,7 +2,7 @@
|
||||
import { extendObservable, action, runInAction, computed } from 'mobx';
|
||||
import invariant from 'invariant';
|
||||
|
||||
import ApiClient, { client } from 'utils/ApiClient';
|
||||
import { client } from 'utils/ApiClient';
|
||||
import stores from 'stores';
|
||||
import ErrorsStore from 'stores/ErrorsStore';
|
||||
|
||||
@ -17,14 +17,13 @@ class Document {
|
||||
html: string;
|
||||
id: string;
|
||||
private: boolean;
|
||||
starred: boolean;
|
||||
team: string;
|
||||
text: string;
|
||||
title: string;
|
||||
updatedAt: string;
|
||||
updatedBy: User;
|
||||
url: string;
|
||||
|
||||
client: ApiClient;
|
||||
errors: ErrorsStore;
|
||||
|
||||
/* Computed */
|
||||
@ -56,7 +55,7 @@ class Document {
|
||||
|
||||
@action update = async () => {
|
||||
try {
|
||||
const res = await this.client.post('/documents.info', { id: this.id });
|
||||
const res = await client.post('/documents.info', { id: this.id });
|
||||
invariant(res && res.data, 'Document API response should be available');
|
||||
const { data } = res;
|
||||
runInAction('Document#update', () => {
|
||||
@ -73,7 +72,6 @@ class Document {
|
||||
|
||||
constructor(document: Document) {
|
||||
this.updateData(document);
|
||||
this.client = client;
|
||||
this.errors = stores.errors;
|
||||
}
|
||||
}
|
||||
|
13
frontend/models/Document.test.js
Normal file
13
frontend/models/Document.test.js
Normal file
@ -0,0 +1,13 @@
|
||||
/* eslint-disable */
|
||||
import Document from './Document';
|
||||
|
||||
describe('Document model', () => {
|
||||
test('should initialize with data', () => {
|
||||
const document = new Document({
|
||||
id: 123,
|
||||
title: 'Onboarding',
|
||||
text: 'Some body text'
|
||||
});
|
||||
expect(document.title).toBe('Onboarding');
|
||||
});
|
||||
});
|
@ -22,7 +22,7 @@ class CollectionsStore {
|
||||
|
||||
/* Actions */
|
||||
|
||||
@action fetch = async (): Promise<*> => {
|
||||
@action fetchAll = async (): Promise<*> => {
|
||||
try {
|
||||
const res = await this.client.post('/collections.list', {
|
||||
id: this.teamId,
|
||||
|
@ -27,7 +27,7 @@ describe('CollectionsStore', () => {
|
||||
})),
|
||||
};
|
||||
|
||||
await store.fetch();
|
||||
await store.fetchAll();
|
||||
|
||||
expect(store.client.post).toHaveBeenCalledWith('/collections.list', {
|
||||
id: 123,
|
||||
@ -44,7 +44,7 @@ describe('CollectionsStore', () => {
|
||||
add: jest.fn(),
|
||||
};
|
||||
|
||||
await store.fetch();
|
||||
await store.fetchAll();
|
||||
|
||||
expect(store.errors.add).toHaveBeenCalledWith(
|
||||
'Failed to load collections'
|
||||
|
83
frontend/stores/DocumentsStore.js
Normal file
83
frontend/stores/DocumentsStore.js
Normal file
@ -0,0 +1,83 @@
|
||||
// @flow
|
||||
import { observable, action, runInAction } from 'mobx';
|
||||
import { client } from 'utils/ApiClient';
|
||||
import _ from 'lodash';
|
||||
import invariant from 'invariant';
|
||||
|
||||
import stores from 'stores';
|
||||
import Document from 'models/Document';
|
||||
import ErrorsStore from 'stores/ErrorsStore';
|
||||
|
||||
type Options = {
|
||||
teamId: string,
|
||||
};
|
||||
|
||||
class DocumentsStore {
|
||||
@observable data: Object = {};
|
||||
@observable isLoaded: boolean = false;
|
||||
errors: ErrorsStore;
|
||||
|
||||
/* Actions */
|
||||
|
||||
@action fetchAll = async (): Promise<*> => {
|
||||
try {
|
||||
const res = await client.post('/documents.list');
|
||||
invariant(res && res.data, 'Document list not available');
|
||||
const { data } = res;
|
||||
runInAction('DocumentsStore#fetchAll', () => {
|
||||
const loaded = _.keyBy(
|
||||
data.map(document => new Document(document)),
|
||||
'id'
|
||||
);
|
||||
this.data = {
|
||||
...this.data,
|
||||
...loaded,
|
||||
};
|
||||
this.isLoaded = true;
|
||||
});
|
||||
} catch (e) {
|
||||
this.errors.add('Failed to load documents');
|
||||
}
|
||||
};
|
||||
|
||||
@action fetch = async (id: string): Promise<*> => {
|
||||
try {
|
||||
const res = await client.post('/documents.info', { id });
|
||||
invariant(res && res.data, 'Document not available');
|
||||
const { data } = res;
|
||||
runInAction('DocumentsStore#fetch', () => {
|
||||
this.update(new Document(data));
|
||||
this.isLoaded = true;
|
||||
});
|
||||
} catch (e) {
|
||||
this.errors.add('Failed to load documents');
|
||||
}
|
||||
};
|
||||
|
||||
@action add = (document: Document): void => {
|
||||
this.data[document.id] = document;
|
||||
};
|
||||
|
||||
@action remove = (id: string): void => {
|
||||
delete this.data[id];
|
||||
};
|
||||
|
||||
@action update = (document: Document): void => {
|
||||
const existing = this.data[document.id];
|
||||
|
||||
this.data[document.id] = {
|
||||
...existing,
|
||||
document,
|
||||
};
|
||||
};
|
||||
|
||||
getById = (id: string): Document => {
|
||||
return _.find(this.data, { id });
|
||||
};
|
||||
|
||||
constructor(options: Options) {
|
||||
this.errors = stores.errors;
|
||||
}
|
||||
}
|
||||
|
||||
export default DocumentsStore;
|
@ -1,6 +1,6 @@
|
||||
// @flow
|
||||
import { observable, action, computed } from 'mobx';
|
||||
import type { Document } from 'types';
|
||||
import Document from 'models/Document';
|
||||
import Collection from 'models/Collection';
|
||||
|
||||
class UiStore {
|
||||
|
@ -2,10 +2,12 @@
|
||||
import React from 'react';
|
||||
import { shallow } from 'enzyme';
|
||||
import toJson from 'enzyme-to-json';
|
||||
import localStorage from '../../__mocks__/localStorage';
|
||||
|
||||
const snap = children => {
|
||||
const wrapper = shallow(children);
|
||||
expect(toJson(wrapper)).toMatchSnapshot();
|
||||
};
|
||||
|
||||
global.localStorage = localStorage;
|
||||
global.snap = snap;
|
||||
|
Reference in New Issue
Block a user