2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../models';
|
2017-02-20 18:32:27 +00:00
|
|
|
import * as db from '../database';
|
2016-09-04 21:32:36 +00:00
|
|
|
|
2016-09-08 06:54:35 +00:00
|
|
|
function loadFixture (name) {
|
2017-02-20 18:32:27 +00:00
|
|
|
const fixtures = require(`../__fixtures__/${name}`).data;
|
2016-09-08 06:54:35 +00:00
|
|
|
const promises = [];
|
|
|
|
for (const type of Object.keys(fixtures)) {
|
|
|
|
for (const doc of fixtures[type]) {
|
|
|
|
promises.push(db.insert(Object.assign({}, doc, {type})));
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
|
|
|
return Promise.all(promises);
|
2016-09-08 06:54:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
describe('init()', () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
beforeEach(global.insomniaBeforeEach);
|
2016-11-10 09:00:29 +00:00
|
|
|
it('handles being initialized twice', async () => {
|
2016-11-16 17:18:39 +00:00
|
|
|
await db.init(models.types(), {inMemoryOnly: true});
|
|
|
|
await db.init(models.types(), {inMemoryOnly: true});
|
2016-11-10 09:00:29 +00:00
|
|
|
expect((await db.all(models.request.type)).length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onChange()', () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
beforeEach(global.insomniaBeforeEach);
|
2016-11-10 09:00:29 +00:00
|
|
|
it('handles change listeners', async () => {
|
|
|
|
const doc = {
|
|
|
|
type: models.request.type,
|
2016-12-01 17:03:33 +00:00
|
|
|
parentId: 'nothing',
|
2016-11-10 09:00:29 +00:00
|
|
|
name: 'foo'
|
|
|
|
};
|
|
|
|
|
|
|
|
const changesSeen = [];
|
|
|
|
const callback = change => {
|
|
|
|
changesSeen.push(change);
|
|
|
|
};
|
|
|
|
db.onChange(callback);
|
|
|
|
|
2016-12-01 17:03:33 +00:00
|
|
|
const newDoc = await models.request.create(doc);
|
|
|
|
const updatedDoc = await models.request.update(newDoc, {name: 'bar'});
|
2016-11-10 09:00:29 +00:00
|
|
|
expect(changesSeen.length).toBe(2);
|
2016-12-01 17:03:33 +00:00
|
|
|
|
2016-11-10 09:00:29 +00:00
|
|
|
expect(changesSeen).toEqual([
|
2016-12-01 17:03:33 +00:00
|
|
|
[[db.CHANGE_INSERT, newDoc, false]],
|
2017-03-03 20:09:08 +00:00
|
|
|
[[db.CHANGE_UPDATE, updatedDoc, false]]
|
2016-11-10 09:00:29 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
db.offChange(callback);
|
2016-12-01 17:03:33 +00:00
|
|
|
await models.request.create(doc);
|
2016-11-10 09:00:29 +00:00
|
|
|
expect(changesSeen.length).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('bufferChanges()', () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
beforeEach(global.insomniaBeforeEach);
|
2016-11-10 09:00:29 +00:00
|
|
|
it('properly buffers changes', async () => {
|
|
|
|
const doc = {
|
|
|
|
type: models.request.type,
|
2016-12-01 17:03:33 +00:00
|
|
|
parentId: 'n/a',
|
2016-11-10 09:00:29 +00:00
|
|
|
name: 'foo'
|
|
|
|
};
|
|
|
|
|
|
|
|
const changesSeen = [];
|
|
|
|
const callback = change => {
|
|
|
|
changesSeen.push(change);
|
|
|
|
};
|
|
|
|
db.onChange(callback);
|
|
|
|
|
|
|
|
db.bufferChanges();
|
2016-12-01 17:03:33 +00:00
|
|
|
const newDoc = await models.request.create(doc);
|
|
|
|
const updatedDoc = await models.request.update(newDoc, true);
|
2016-11-10 09:00:29 +00:00
|
|
|
|
|
|
|
// Assert no change seen before flush
|
|
|
|
expect(changesSeen.length).toBe(0);
|
|
|
|
|
|
|
|
// Assert changes seen after flush
|
|
|
|
db.flushChanges();
|
|
|
|
expect(changesSeen).toEqual([[
|
2016-12-01 17:03:33 +00:00
|
|
|
[db.CHANGE_INSERT, newDoc, false],
|
|
|
|
[db.CHANGE_UPDATE, updatedDoc, false]
|
2016-11-10 09:00:29 +00:00
|
|
|
]]);
|
|
|
|
|
|
|
|
// Assert no more changes seen after flush again
|
|
|
|
db.flushChanges();
|
|
|
|
expect(changesSeen).toEqual([[
|
2016-12-01 17:03:33 +00:00
|
|
|
[db.CHANGE_INSERT, newDoc, false],
|
|
|
|
[db.CHANGE_UPDATE, updatedDoc, false]
|
2016-11-10 09:00:29 +00:00
|
|
|
]]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-09-04 21:32:36 +00:00
|
|
|
describe('requestCreate()', () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
beforeEach(global.insomniaBeforeEach);
|
2016-10-02 20:57:00 +00:00
|
|
|
it('creates a valid request', async () => {
|
2016-09-04 21:32:36 +00:00
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
const patch = {
|
|
|
|
name: 'My Request',
|
|
|
|
parentId: 'wrk_123'
|
|
|
|
};
|
|
|
|
|
2016-11-10 01:15:27 +00:00
|
|
|
const r = await models.request.create(patch);
|
2017-06-01 13:54:46 +00:00
|
|
|
expect(Object.keys(r).length).toBe(18);
|
2016-09-04 21:32:36 +00:00
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
expect(r._id).toMatch(/^req_[a-zA-Z0-9]{32}$/);
|
2016-10-02 20:57:00 +00:00
|
|
|
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');
|
2016-11-22 19:42:10 +00:00
|
|
|
expect(r.body).toEqual({});
|
2016-10-02 20:57:00 +00:00
|
|
|
expect(r.parameters).toEqual([]);
|
|
|
|
expect(r.headers).toEqual([]);
|
|
|
|
expect(r.authentication).toEqual({});
|
|
|
|
expect(r.metaSortKey).toBeLessThanOrEqual(-1 * now);
|
|
|
|
expect(r.parentId).toBe('wrk_123');
|
2016-09-04 21:32:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when missing parentID', () => {
|
2016-11-10 01:15:27 +00:00
|
|
|
const fn = () => models.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()', () => {
|
2016-10-02 20:57:00 +00:00
|
|
|
beforeEach(async () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
await global.insomniaBeforeEach();
|
2016-10-02 20:57:00 +00:00
|
|
|
await loadFixture('nestedfolders');
|
2016-09-08 06:54:35 +00:00
|
|
|
});
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
it('duplicates a RequestGroup', async () => {
|
2016-11-10 01:15:27 +00:00
|
|
|
const requestGroup = await models.requestGroup.getById('fld_1');
|
2016-10-02 20:57:00 +00:00
|
|
|
expect(requestGroup.name).toBe('Fld 1');
|
2016-09-08 06:54:35 +00:00
|
|
|
|
2016-11-10 01:15:27 +00:00
|
|
|
const newRequestGroup = await models.requestGroup.duplicate(requestGroup);
|
2016-10-02 20:57:00 +00:00
|
|
|
expect(newRequestGroup._id).not.toBe(requestGroup._id);
|
|
|
|
expect(newRequestGroup.name).toBe('Fld 1 (Copy)');
|
2016-09-08 06:54:35 +00:00
|
|
|
|
2016-11-10 01:15:27 +00:00
|
|
|
const allRequests = await models.request.all();
|
|
|
|
const allRequestGroups = await models.requestGroup.all();
|
|
|
|
const childRequests = await models.request.findByParentId(requestGroup._id);
|
|
|
|
const childRequestGroups = await models.requestGroup.findByParentId(requestGroup._id);
|
|
|
|
const newChildRequests = await models.request.findByParentId(newRequestGroup._id);
|
|
|
|
const newChildRequestGroups = await models.requestGroup.findByParentId(newRequestGroup._id);
|
2016-10-02 20:57:00 +00:00
|
|
|
// 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);
|
2016-09-08 06:54:35 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
expect(childRequests.length).toBe(2);
|
|
|
|
expect(childRequestGroups.length).toBe(1);
|
2016-09-08 06:54:35 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
expect(newChildRequests.length).toBe(2);
|
|
|
|
expect(newChildRequestGroups.length).toBe(1);
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
2016-09-08 06:54:35 +00:00
|
|
|
});
|