Skip to content

Commit

Permalink
test: nucleus/src/object done (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-m-dev committed Nov 1, 2022
1 parent 204d2df commit afebec0
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 330 deletions.
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ https://github.com/qlik-oss/nebula.js/blob/master/.github/CONTRIBUTING.md#git

<!-- Write your motivation here -->


## Requirements checklist

<!-- Make sure you got these covered -->

- [ ] Api specification
- [ ] Ran `yarn spec`
- [ ] No changes ***OR*** API changes has been formally approved
- [ ] No changes **_OR_** API changes has been formally approved
- [ ] Unit/Component test coverage
- [ ] Correct PR title for the changes (fix, chore, feat)

When build and tests have passed:

- [ ] Add code reviewers, for example @qlik-oss/nebula-core
120 changes: 120 additions & 0 deletions apis/nucleus/src/object/__tests__/create-session-object.inspect.js
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 apis/nucleus/src/object/__tests__/create-session-object.spec.js

This file was deleted.

54 changes: 54 additions & 0 deletions apis/nucleus/src/object/__tests__/get-object.inspect.js
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
);
});
});
45 changes: 0 additions & 45 deletions apis/nucleus/src/object/__tests__/get-object.spec.js

This file was deleted.

Loading

0 comments on commit afebec0

Please sign in to comment.