Addressed user feedback
This commit is contained in:
@@ -3,6 +3,7 @@ import TestServer from 'fetch-test-server';
|
||||
import app from '..';
|
||||
import { View, Star } from '../models';
|
||||
import { flushdb, seed } from '../test/support';
|
||||
import Document from '../models/Document';
|
||||
|
||||
const server = new TestServer(app.callback());
|
||||
|
||||
@@ -194,6 +195,49 @@ describe('#documents.unstar', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.create', async () => {
|
||||
it('should create as a new document', async () => {
|
||||
const { user, collection } = await seed();
|
||||
const res = await server.post('/api/documents.create', {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
collection: collection.id,
|
||||
title: 'new document',
|
||||
text: 'hello',
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
const newDocument = await Document.findOne({
|
||||
where: {
|
||||
id: body.data.id,
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(newDocument.parentDocumentId).toBe(null);
|
||||
expect(newDocument.collection.id).toBe(collection.id);
|
||||
});
|
||||
|
||||
it('should create as a child', async () => {
|
||||
const { user, document, collection } = await seed();
|
||||
const res = await server.post('/api/documents.create', {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
collection: collection.id,
|
||||
title: 'new document',
|
||||
text: 'hello',
|
||||
parentDocument: document.id,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.title).toBe('new document');
|
||||
expect(body.data.collection.documents.length).toBe(2);
|
||||
expect(body.data.collection.documents[1].children[0].id).toBe(body.data.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.update', async () => {
|
||||
it('should update document details in the root', async () => {
|
||||
const { user, document } = await seed();
|
||||
|
||||
@@ -133,16 +133,23 @@ Collection.prototype.addDocumentToStructure = async function(document, index) {
|
||||
// Sequelize doesn't seem to set the value with splice on JSONB field
|
||||
this.documentStructure = this.documentStructure;
|
||||
} else {
|
||||
this.documentStructure = this.documentStructure.map(childDocument => {
|
||||
if (document.parentDocumentId === childDocument.id) {
|
||||
childDocument.children.splice(
|
||||
index || childDocument.children.length,
|
||||
0,
|
||||
document.toJSON()
|
||||
);
|
||||
}
|
||||
return childDocument;
|
||||
});
|
||||
// Recursively place document
|
||||
const placeDocument = documentList => {
|
||||
return documentList.map(childDocument => {
|
||||
if (document.parentDocumentId === childDocument.id) {
|
||||
childDocument.children.splice(
|
||||
index || childDocument.children.length,
|
||||
0,
|
||||
document.toJSON()
|
||||
);
|
||||
} else {
|
||||
childDocument.children = placeDocument(childDocument.children);
|
||||
}
|
||||
|
||||
return childDocument;
|
||||
});
|
||||
};
|
||||
this.documentStructure = placeDocument(this.documentStructure);
|
||||
}
|
||||
|
||||
await this.save();
|
||||
|
||||
Reference in New Issue
Block a user