insomnia/app/backend/__tests__/database.test.js

105 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-09-21 00:03:26 +00:00
'use strict';
const db = require('../database');
2016-09-21 00:18:18 +00:00
const {PREVIEW_MODE_SOURCE} = require('../previewModes');
2016-09-04 21:32:36 +00:00
2016-09-08 06:54:35 +00:00
function loadFixture (name) {
const fixtures = require(`../__fixtures__/${name}`);
const promises = [];
for (const type of Object.keys(fixtures)) {
for (const doc of fixtures[type]) {
promises.push(db.insert(Object.assign({}, doc, {type})));
}
}
}
2016-09-04 21:32:36 +00:00
describe('requestCreate()', () => {
beforeEach(() => {
2016-09-08 06:54:35 +00:00
return db.initDB({inMemoryOnly: true}, true);
2016-09-04 21:32:36 +00:00
});
it('creates a valid request', () => {
const now = Date.now();
const patch = {
name: 'My Request',
parentId: 'wrk_123'
};
2016-09-21 20:49:54 +00:00
return db.request.create(patch).then(r => {
2016-09-09 00:33:34 +00:00
expect(Object.keys(r).length).toBe(15);
2016-09-04 21:32:36 +00:00
expect(r._id).toMatch(/^req_[a-zA-Z0-9]{24}$/);
expect(r.created).toBeGreaterThanOrEqual(now);
expect(r.modified).toBeGreaterThanOrEqual(now);
expect(r.type).toBe('Request');
expect(r.name).toBe('My Request');
expect(r.url).toBe('');
expect(r.method).toBe('GET');
expect(r.body).toBe('');
expect(r.parameters).toEqual([]);
expect(r.headers).toEqual([]);
expect(r.authentication).toEqual({});
expect(r.metaSortKey).toBeLessThanOrEqual(-1 * now);
expect(r.metaPreviewMode).toEqual(PREVIEW_MODE_SOURCE);
expect(r.parentId).toBe('wrk_123');
});
});
it('throws when missing parentID', () => {
2016-09-21 20:49:54 +00:00
const fn = () => db.request.create({name: 'My Request'});
2016-09-04 21:32:36 +00:00
expect(fn).toThrowError('New Requests missing `parentId`');
});
});
2016-09-08 06:54:35 +00:00
describe('requestGroupDuplicate()', () => {
beforeEach(() => {
return Promise.all([
db.initDB({inMemoryOnly: true}, true),
loadFixture('nestedfolders')
]);
});
it('duplicates a RequestGroup', () => {
return new Promise((resolve, reject) => {
2016-09-21 20:49:54 +00:00
db.requestGroup.getById('fld_1').then(requestGroup => {
2016-09-08 06:54:35 +00:00
expect(requestGroup.name).toBe('Fld 1');
2016-09-21 20:49:54 +00:00
db.requestGroup.duplicate(requestGroup).then(newRequestGroup => {
2016-09-08 06:54:35 +00:00
expect(newRequestGroup._id).not.toBe(requestGroup._id);
expect(newRequestGroup.name).toBe('Fld 1 (Copy)');
Promise.all([
2016-09-21 20:49:54 +00:00
db.request.all(),
db.requestGroup.all(),
db.request.findByParentId(requestGroup._id),
db.requestGroup.findByParentId(requestGroup._id),
db.request.findByParentId(newRequestGroup._id),
db.requestGroup.findByParentId(newRequestGroup._id)
2016-09-08 06:54:35 +00:00
]).then(([
allRequests,
allRequestGroups,
childRequests,
childRequestGroups,
newChildRequests,
newChildRequestGroups
]) => {
// This asserting is pretty garbage but it at least checks
// to see that the recursion worked (for the most part)
expect(allRequests.length).toBe(8);
expect(allRequestGroups.length).toBe(5);
expect(childRequests.length).toBe(2);
expect(childRequestGroups.length).toBe(1);
expect(newChildRequests.length).toBe(2);
expect(newChildRequestGroups.length).toBe(1);
resolve();
}, reject);
}, reject)
}, reject);
})
})
});