-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test:
nucleus/src/object
done (#980)
- Loading branch information
Showing
11 changed files
with
333 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
apis/nucleus/src/object/__tests__/create-session-object.inspect.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import * as populatorModule from '../populator'; | ||
import * as initiateModule from '../initiate'; | ||
import create from '../create-session-object'; | ||
|
||
describe('create-session-object', () => { | ||
let halo = {}; | ||
let types; | ||
let sn; | ||
let merged; | ||
let populator; | ||
let init; | ||
let objectModel; | ||
|
||
beforeEach(() => { | ||
populator = jest.fn(); | ||
init = jest.fn(); | ||
|
||
jest.spyOn(populatorModule, 'default').mockImplementation(populator); | ||
jest.spyOn(initiateModule, 'default').mockImplementation(init); | ||
objectModel = { id: 'id', on: () => {}, once: () => {} }; | ||
types = { | ||
get: jest.fn(), | ||
}; | ||
halo = { | ||
app: { | ||
createSessionObject: jest.fn().mockResolvedValue(objectModel), | ||
}, | ||
types, | ||
}; | ||
|
||
init.mockReturnValue('api'); | ||
|
||
sn = { qae: { properties: { onChange: jest.fn() } } }; | ||
merged = { m: 'true' }; | ||
const t = { | ||
initialProperties: jest.fn().mockResolvedValue(merged), | ||
supernova: jest.fn().mockResolvedValue(sn), | ||
}; | ||
types.get.mockReturnValue(t); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('should call types.get with name and version', () => { | ||
create({ type: 't', version: 'v', fields: 'f' }, halo); | ||
expect(types.get).toHaveBeenCalledWith({ name: 't', version: 'v' }); | ||
}); | ||
|
||
test('should call initialProperties on returned type', () => { | ||
const t = { initialProperties: jest.fn() }; | ||
t.initialProperties.mockReturnValue({ then: () => {} }); | ||
types.get.mockReturnValue(t); | ||
create({ type: 't', version: 'v', fields: 'f', properties: 'props' }, halo); | ||
expect(t.initialProperties).toHaveBeenCalledWith('props'); | ||
}); | ||
|
||
test('should populate fields', async () => { | ||
await create({ type: 't', version: 'v', fields: 'f', properties: 'props' }, halo); | ||
expect(populator).toHaveBeenCalledWith({ sn, properties: merged, fields: 'f' }, halo); | ||
}); | ||
|
||
test('should call properties onChange handler when optional props are provided', async () => { | ||
await create({ type: 't', version: 'v', fields: 'f', properties: 'props' }, halo); | ||
expect(sn.qae.properties.onChange).toHaveBeenCalledWith(merged); | ||
}); | ||
|
||
test('should not call onChange handler when optional props are not provided', async () => { | ||
await create({ type: 't', version: 'v', fields: 'f' }, halo); | ||
expect(sn.qae.properties.onChange).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('should create a session object with merged props', async () => { | ||
await create({ type: 't', version: 'v', fields: 'f', properties: 'props' }, halo); | ||
expect(halo.app.createSessionObject).toHaveBeenCalledWith(merged); | ||
}); | ||
|
||
test('should create a dummy session object when error is thrown', async () => { | ||
types.get.mockImplementation(() => { | ||
throw new Error('oops'); | ||
}); | ||
await create({ type: 't', version: 'v', fields: 'f', properties: 'props' }, halo); | ||
expect(halo.app.createSessionObject).toHaveBeenCalledWith({ | ||
qInfo: { qType: 't' }, | ||
visualization: 't', | ||
}); | ||
}); | ||
|
||
test('should call init', async () => { | ||
const ret = await create( | ||
{ type: 't', version: 'v', fields: 'f', properties: 'props', options: 'a', plugins: [] }, | ||
halo | ||
); | ||
expect(ret).toBe('api'); | ||
expect(init).toHaveBeenCalledWith( | ||
objectModel, | ||
{ options: 'a', plugins: [], element: undefined }, | ||
halo, | ||
undefined, | ||
expect.any(Function) | ||
); | ||
}); | ||
|
||
test('should catch and pass error', async () => { | ||
const err = new Error('oops'); | ||
types.get.mockReturnValue(err); | ||
const optional = { properties: 'props', element: 'el', options: 'opts' }; | ||
const ret = await create({ type: 't', ...optional }, halo); | ||
expect(ret).toBe('api'); | ||
expect(init).toHaveBeenCalledWith( | ||
objectModel, | ||
{ options: 'opts', plugins: undefined, element: 'el' }, | ||
halo, | ||
expect.objectContaining(err), | ||
expect.any(Function) | ||
); | ||
}); | ||
}); |
122 changes: 0 additions & 122 deletions
122
apis/nucleus/src/object/__tests__/create-session-object.spec.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as initiateModule from '../initiate'; | ||
import create from '../get-object'; | ||
|
||
describe('get-object', () => { | ||
let context = {}; | ||
let init; | ||
let objectModel; | ||
let model; | ||
|
||
beforeEach(() => { | ||
init = jest.fn(); | ||
jest.spyOn(initiateModule, 'default').mockImplementation(init); | ||
|
||
model = { | ||
id: 'model-x', | ||
on: jest.fn(), | ||
once: jest.fn(), | ||
}; | ||
objectModel = jest.fn(); | ||
init.mockReturnValue('api'); | ||
context = { app: { id: 'appid', getObject: async () => Promise.resolve(objectModel) } }; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('should get object from app only once', async () => { | ||
objectModel.mockReturnValue(model); | ||
const spy = jest.spyOn(context.app, 'getObject').mockImplementation(objectModel); | ||
await create({ id: 'x' }, context); | ||
await create({ id: 'x' }, context); | ||
await create({ id: 'x' }, context); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(spy).toHaveBeenCalledWith('x'); | ||
}); | ||
|
||
test('should call init', async () => { | ||
objectModel.mockReturnValue(model); | ||
jest.spyOn(context.app, 'getObject').mockImplementation(objectModel); | ||
const ret = await create({ id: 'x', options: 'op', plugins: [], element: 'el' }, context); | ||
expect(ret).toBe('api'); | ||
expect(init).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
id: model.id, | ||
on: expect.any(Function), | ||
once: expect.any(Function), | ||
}), | ||
{ options: 'op', plugins: [], element: 'el' }, | ||
context | ||
); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.