2020-10-28 00:05:54 +00:00
|
|
|
import * as models from '../index';
|
|
|
|
import { globalBeforeEach } from '../../__jest__/before-each';
|
|
|
|
|
|
|
|
describe('init()', () => {
|
|
|
|
beforeEach(globalBeforeEach);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
it('contains all required fields', async () => {
|
|
|
|
expect(models.grpcRequestMeta.init()).toEqual({
|
|
|
|
pinned: false,
|
2020-10-28 22:23:07 +00:00
|
|
|
lastActive: 0,
|
2020-10-28 00:05:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('create()', () => {
|
|
|
|
beforeEach(globalBeforeEach);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2020-10-28 00:05:54 +00:00
|
|
|
it('creates a valid GrpcRequest', async () => {
|
|
|
|
Date.now = jest.fn().mockReturnValue(1478795580200);
|
|
|
|
const request = await models.grpcRequestMeta.create({
|
|
|
|
pinned: true,
|
|
|
|
parentId: 'greq_124',
|
|
|
|
});
|
|
|
|
const expected = {
|
|
|
|
_id: 'greqm_cc1dd2ca4275747aa88199e8efd42403',
|
|
|
|
created: 1478795580200,
|
|
|
|
modified: 1478795580200,
|
|
|
|
parentId: 'greq_124',
|
|
|
|
pinned: true,
|
|
|
|
type: 'GrpcRequestMeta',
|
2020-10-28 22:23:07 +00:00
|
|
|
lastActive: 0,
|
2020-10-28 00:05:54 +00:00
|
|
|
};
|
|
|
|
expect(request).toEqual(expected);
|
|
|
|
expect(await models.grpcRequestMeta.getOrCreateByParentId(expected.parentId)).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates a valid GrpcRequestMeta if it does not exist', async () => {
|
|
|
|
Date.now = jest.fn().mockReturnValue(1478795580200);
|
|
|
|
const request = await models.grpcRequestMeta.getOrCreateByParentId('greq_124');
|
|
|
|
const expected = {
|
|
|
|
_id: 'greqm_dd2ccc1a2745477a881a9e8ef9d42403',
|
|
|
|
created: 1478795580200,
|
|
|
|
modified: 1478795580200,
|
|
|
|
parentId: 'greq_124',
|
|
|
|
pinned: false,
|
|
|
|
type: 'GrpcRequestMeta',
|
2020-10-28 22:23:07 +00:00
|
|
|
lastActive: 0,
|
2020-10-28 00:05:54 +00:00
|
|
|
};
|
|
|
|
expect(request).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when missing parentId', async () => {
|
2021-05-12 06:35:00 +00:00
|
|
|
expect(() =>
|
|
|
|
models.grpcRequestMeta.create({
|
|
|
|
pinned: true,
|
|
|
|
}),
|
|
|
|
).toThrow('New GrpcRequestMeta missing `parentId`');
|
2020-10-28 00:05:54 +00:00
|
|
|
});
|
2020-10-28 22:23:07 +00:00
|
|
|
|
|
|
|
it('fails when parentId prefix is not that of a GrpcRequest', async () => {
|
2021-05-12 06:35:00 +00:00
|
|
|
expect(() =>
|
|
|
|
models.grpcRequestMeta.create({
|
|
|
|
parentId: 'req_123',
|
|
|
|
}),
|
|
|
|
).toThrow('Expected the parent of GrpcRequestMeta to be a GrpcRequest');
|
2020-10-28 22:23:07 +00:00
|
|
|
});
|
2020-10-28 00:05:54 +00:00
|
|
|
});
|