Skip to content

Commit

Permalink
added some specs to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
konzz committed Dec 17, 2018
1 parent ba553f1 commit 013f741
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 40 deletions.
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ ratings:
- app/**
exclude_patterns:
- "**/index.js"
- "**/fixtures.js"
22 changes: 6 additions & 16 deletions app/api/templates/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,13 @@ export default (app) => {
_id: Joi.string().required()
})),
(req, res, next) => {
templates.get()
.then((_templates) => {
const templateToBeDefault = _templates.find(t => t._id.toString() === req.body._id);
const currentDefault = _templates.find(t => t.default);
templateToBeDefault.default = true;
if (currentDefault) {
currentDefault.default = false;
templates.save(currentDefault).then((response) => {
req.io.sockets.emit('templateChange', response);
});
templates.setAsDefault(req.body._id)
.then(([newDefault, oldDefault]) => {
req.io.sockets.emit('templateChange', newDefault);
if (oldDefault) {
req.io.sockets.emit('templateChange', oldDefault);
}

templates.save(templateToBeDefault)
.then((response) => {
res.json(response);
req.io.sockets.emit('templateChange', response);
});
res.json(newDefault);
})
.catch(next);
});
Expand Down
17 changes: 17 additions & 0 deletions app/api/templates/specs/__snapshots__/routes.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`templates routes /api/templates/setasdefault should have a validation schema 1`] = `
Object {
"children": Object {
"_id": Object {
"flags": Object {
"presence": "required",
},
"invalids": Array [
"",
],
"type": "string",
},
},
"type": "object",
}
`;

exports[`templates routes /templates/count_by_thesauri should have a validation schema 1`] = `
Object {
"children": Object {
Expand Down
8 changes: 4 additions & 4 deletions app/api/templates/specs/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const templateToBeDeleted = '589af97080fc0b23471d67f1';
const templateWithContents = db.id();
export default {
templates: [
{_id: templateToBeEditedId, name: 'template to be edited'},
{_id: templateToBeEditedId, name: 'template to be edited', default: true},
{_id: db.id(templateToBeDeleted), name: 'to be deleted'},
{_id: db.id(), name: 'duplicated name'},
{_id: db.id(), name: 'thesauri template', properties: [{type: 'select', content: 'thesauri1'}]},
{_id: db.id(), name: 'thesauri template 2', properties: [{type: 'select', content: 'thesauri1'}]},
{_id: templateWithContents, name: 'content template', properties: [{id: '1', type: 'select', content: 'thesauri1'}, {id: '2', type: 'multiselect', content: 'thesauri2'}]}
{_id: db.id(), name: 'thesauri template', properties: [{type: 'select', content: 'thesauri1', label: 'select'}]},
{_id: db.id(), name: 'thesauri template 2', properties: [{type: 'select', content: 'thesauri1', label: 'select2'}]},
{_id: templateWithContents, name: 'content template', properties: [{id: '1', type: 'select', content: 'thesauri1', label: 'select3'}, {id: '2', type: 'multiselect', content: 'thesauri2', label: 'select4'}]}
]
};

Expand Down
21 changes: 21 additions & 0 deletions app/api/templates/specs/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,25 @@ describe('templates routes', () => {
.catch(catchErrors(done));
});
});

describe('/api/templates/setasdefault', () => {
it('should have a validation schema', () => {
expect(routes.post.validation('/api/templates/setasdefault')).toMatchSnapshot();
});

it('should call templates to set the new default', (done) => {
spyOn(templates, 'setAsDefault').and.returnValue(Promise.resolve([{ name: 'newDefault' }, { name: 'oldDefault' }]));
const emit = jasmine.createSpy('emit');
const req = { body: { _id: 'abc1' }, io: { sockets: { emit } } };
routes.post('/api/templates/setasdefault', req)
.then((result) => {
expect(result).toEqual({ name: 'newDefault' });
expect(templates.setAsDefault).toHaveBeenCalledWith('abc1');
expect(emit).toHaveBeenCalledWith('templateChange', { name: 'newDefault' });
expect(emit).toHaveBeenCalledWith('templateChange', { name: 'oldDefault' });
done();
})
.catch(catchErrors(done));
});
});
});
63 changes: 43 additions & 20 deletions app/api/templates/specs/templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ describe('templates', () => {
spyOn(translations, 'updateContext');
spyOn(entities, 'removeValuesFromEntities');
spyOn(entities, 'updateMetadataProperties').and.returnValue(Promise.resolve());
const changedTemplate = { _id: templateWithContents,
name: 'changed',
properties:
const changedTemplate = {
_id: templateWithContents,
name: 'changed',
properties:
[{ id: '1', type: 'select', content: 'new_thesauri', label: 'select' },
{ id: '2', type: 'multiselect', content: 'new_thesauri', label: 'multiselect' }] };
{ id: '2', type: 'multiselect', content: 'new_thesauri', label: 'multiselect' }]
};

templates.save(changedTemplate)
.then(() => {
Expand All @@ -68,14 +70,16 @@ properties:
});

it('should validate properties not having repeated names and return an error', (done) => {
const newTemplate = { name: 'created_template',
properties: [
{ label: 'label 1' },
{ label: 'label 1' },
{ label: 'Label 2' },
{ label: 'label 2' },
{ label: 'label 3' }
] };
const newTemplate = {
name: 'created_template',
properties: [
{ label: 'label 1' },
{ label: 'label 1' },
{ label: 'Label 2' },
{ label: 'label 2' },
{ label: 'label 3' }
]
};

templates.save(newTemplate)
.then(() => done.fail('properties have repeated names, should have failed with an error'))
Expand All @@ -87,11 +91,13 @@ properties: [
});

it('should add it to translations with Entity type', (done) => {
const newTemplate = { name: 'created template',
properties: [
{ label: 'label 1' },
{ label: 'label 2' }
] };
const newTemplate = {
name: 'created template',
properties: [
{ label: 'label 1' },
{ label: 'label 2' }
]
};

templates.save(newTemplate)
.then((response) => {
Expand All @@ -107,14 +113,16 @@ properties: [
});

it('should assign a safe property name based on the label ', (done) => {
const newTemplate = { name: 'created_template',
const newTemplate = {
name: 'created_template',
properties: [
{ label: 'label 1' },
{ label: 'label 2' },
{ label: 'label 3' },
{ label: 'label 4', name: 'name' },
{ label: 'label 5', type: 'geolocation' }
] };
]
};

templates.save(newTemplate)
.then(() => templates.get())
Expand Down Expand Up @@ -151,7 +159,7 @@ properties: [

it('should updateMetadataProperties', (done) => {
spyOn(translations, 'updateContext');
const template = { _id: templateToBeEditedId, name: 'template to be edited', properties: [] };
const template = { _id: templateToBeEditedId, name: 'template to be edited', properties: [], default: true };
const toSave = { _id: templateToBeEditedId, name: 'changed name' };
templates.save(toSave, 'en')
.then(() => {
Expand Down Expand Up @@ -292,4 +300,19 @@ properties: [
.catch(catchErrors(done));
});
});

describe('setAsDefault()', () => {
beforeEach(() => {
spyOn(translations, 'updateContext');
});
it('should set the given ID as the default template', (done) => {
templates.setAsDefault(templateWithContents.toString())
.then(([newDefault, oldDefault]) => {
expect(newDefault.name).toBe('content template');
expect(oldDefault.name).toBe('template to be edited');
done();
})
.catch(catchErrors(done));
});
});
});
17 changes: 17 additions & 0 deletions app/api/templates/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ export default {
return model.get(query);
},

setAsDefault(templateId) {
return this.get()
.then((_templates) => {
const templateToBeDefault = _templates.find(t => t._id.toString() === templateId);
const currentDefault = _templates.find(t => t.default);
templateToBeDefault.default = true;
let saveCurrentDefault = Promise.resolve();
if (currentDefault) {
currentDefault.default = false;
saveCurrentDefault = this.save(currentDefault);
}

return Promise.all([this.save(templateToBeDefault), saveCurrentDefault]);
})
.catch(console.log);
},

getById(templateId) {
return model.getById(templateId);
},
Expand Down
19 changes: 19 additions & 0 deletions app/react/Library/helpers/specs/blankState.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { store } from 'app/store';
import Immutable from 'immutable';
import blankState from '../blankState';

describe('blankState()', () => {
describe('when there is no thesauris', () => {
it('it should return true', () => {
spyOn(store, 'getState').and.returnValue({ thesauris: [] });
expect(blankState()).toBe(true);
});
});

describe('when there is thesauris', () => {
it('it should return true', () => {
spyOn(store, 'getState').and.returnValue({ thesauris: [Immutable.fromJS({ values: [{ a: 'a' }] })] });
expect(blankState()).toBe(false);
});
});
});

0 comments on commit 013f741

Please sign in to comment.