Skip to content

Commit

Permalink
test: hc-handler (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
miralemd committed Dec 3, 2019
1 parent feaea90 commit 0f5cb2e
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 55 deletions.
213 changes: 213 additions & 0 deletions apis/nucleus/src/object/__tests__/hc-handler.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
const doMock = () => aw.mock([['**/uid.js', () => () => 'uid']], ['../hc-handler.js']);

describe('hc-handler', () => {
let h;
let hc;
let def;
let handler;
before(() => {
[{ default: handler }] = doMock();
});

beforeEach(() => {
hc = {};
def = {
dimensions: {
added: sinon.stub(),
removed: sinon.stub(),
min: () => 0,
max: () => 2,
},
measures: {
added: sinon.stub(),
removed: sinon.stub(),
min: () => 0,
max: () => 3,
},
};

h = handler({
hc,
def,
properties: 'props',
});
});

describe('add dimension', () => {
it('from string', () => {
h.addDimension('A');
expect(hc.qDimensions).to.eql([
{
qDef: {
cId: 'uid',
qFieldDefs: ['A'],
qSortCriterias: [
{
qSortByLoadOrder: 1,
qSortByNumeric: 1,
qSortByAscii: 1,
},
],
},
},
]);
});

it('from object', () => {
h.addDimension({
qTotalLabel: 'total',
});
expect(hc.qDimensions).to.eql([
{
qDef: {
cId: 'uid',
},
qTotalLabel: 'total',
},
]);
});

it('should not add more than 2', () => {
h.addDimension('A');
h.addDimension('B');
h.addDimension('C');
expect(hc.qDimensions.length).to.eql(2);
});

it('should call added hook on definition', () => {
h.addDimension({ a: 'b' });
expect(def.dimensions.added).to.have.been.calledWithExactly(
{
a: 'b',
qDef: { cId: 'uid' },
},
'props'
);
});

it('should add overflow to layoutExclude', () => {
h.addDimension('A');
h.addDimension('B');
h.addDimension({ a: '=a' });
expect(hc.qLayoutExclude.qHyperCubeDef.qDimensions).to.eql([{ a: '=a', qDef: { cId: 'uid' } }]);
});

it('should update qInterColumnSortOrder', () => {
h.addDimension('A');
h.addDimension('B');
expect(hc.qInterColumnSortOrder).to.eql([0, 1]);
});
});

describe('remove dimension', () => {
beforeEach(() => {
hc.qDimensions = ['a', 'b', 'c'];
hc.qInterColumnSortOrder = [2, 1, 0];
});

it('by index', () => {
h.removeDimension(1);
expect(hc.qDimensions).to.eql(['a', 'c']);
});

it('should call removed hook on definition', () => {
h.removeDimension(1);
expect(def.dimensions.removed).to.have.been.calledWithExactly('b', 'props', 1);
});

it('should update qInterColumnSortOrder', () => {
h.removeDimension(1);
expect(hc.qInterColumnSortOrder).to.eql([1, 0]);
});
});

describe('add measure', () => {
it('from string', () => {
h.addMeasure('A');
expect(hc.qMeasures).to.eql([
{
qDef: {
cId: 'uid',
qDef: 'A',
},
qSortBy: {
qSortByLoadOrder: 1,
qSortByNumeric: -1,
},
},
]);
});

it('from object', () => {
h.addMeasure({
bla: 'meh',
});
expect(hc.qMeasures).to.eql([
{
qDef: {
cId: 'uid',
},
bla: 'meh',
},
]);
});

it('should not add more than 3', () => {
h.addMeasure('A');
h.addMeasure('B');
h.addMeasure('C');
h.addMeasure('D');
expect(hc.qMeasures.length).to.eql(3);
});

it('should call added hook on definition', () => {
h.addMeasure({ a: 'b' });
expect(def.measures.added).to.have.been.calledWithExactly(
{
a: 'b',
qDef: { cId: 'uid' },
},
'props'
);
});

it('should add overflow to layoutExclude', () => {
h.addMeasure('A');
h.addMeasure('B');
h.addMeasure('C');
h.addMeasure({ a: '=a' });
expect(hc.qLayoutExclude.qHyperCubeDef.qMeasures).to.eql([{ a: '=a', qDef: { cId: 'uid' } }]);
});

it('should update qInterColumnSortOrder', () => {
hc.qDimensions = ['a', 'b'];
hc.qInterColumnSortOrder = [0, 1];
h.addMeasure('m1');
h.addMeasure('m2');
expect(hc.qInterColumnSortOrder).to.eql([0, 1, 2, 3]);
});
});

describe('remove measure', () => {
beforeEach(() => {
hc.qDimensions = ['a'];
hc.qMeasures = ['b', 'c', 'd'];
hc.qInterColumnSortOrder = [2, 1, 0, 3];
});

it('by index', () => {
h.removeMeasure(1);
expect(hc.qMeasures).to.eql(['b', 'd']);
});

it('should call removed hook on definition', () => {
h.removeMeasure(1);
expect(def.measures.removed).to.have.been.calledWithExactly('c', 'props', 1);
});

it('should update qInterColumnSortOrder', () => {
h.removeMeasure(1);
expect(hc.qInterColumnSortOrder).to.eql([1, 0, 2]);
});
});
});
104 changes: 104 additions & 0 deletions apis/nucleus/src/object/__tests__/populator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const doMock = ({ handler } = {}) => aw.mock([['**/hc-handler.js', () => handler]], ['../populator.js']);
describe('populator', () => {
let handler;
let context;
let populate;
let sb;
let h;
let ft;
before(() => {
sb = sinon.createSandbox();
h = {
addMeasure: sb.stub(),
addDimension: sb.stub(),
};
const fn = () => h;
handler = sb.spy(fn);
context = {
logger: {
warn: sb.stub(),
},
};
[{ default: populate, fieldType: ft }] = doMock({ handler });
});
beforeEach(() => {
sb.reset();
});

it('should not throw if fields are not provided', () => {
const fn = () => populate({ sn: null, properties: {}, fields: [] }, context);
expect(fn).to.not.throw();
});

it('should log warning if fields is provided but targets are not specified', () => {
const sn = { qae: { data: { targets: [] } } };
populate({ sn, properties: {}, fields: [1] }, context);
expect(context.logger.warn).to.have.been.calledWithExactly(
'Attempting to add fields to an object without a specified data target'
);
});

it('should initiate handler with resolved target', () => {
const target = {
propertyPath: 'a/b/c',
};
const sn = {
qae: {
data: {
targets: [target],
},
},
};
const resolved = { qDimensions: [] };
populate({ sn, properties: { a: { b: { c: resolved } } }, fields: [1] }, context);
expect(handler).to.have.been.calledWithExactly({
hc: resolved,
def: target,
properties: { a: { b: { c: resolved } } },
});
});

it('should add dimension', () => {
const target = {
propertyPath: 'hc',
};
const sn = {
qae: {
data: {
targets: [target],
},
},
};
const resolved = { qDimensions: [] };
populate({ sn, properties: { a: { b: { c: resolved } } }, fields: ['A'] }, context);
expect(h.addDimension).to.have.been.calledWithExactly('A');
});

it('should add measure', () => {
const target = {
propertyPath: 'hc',
};
const sn = {
qae: {
data: {
targets: [target],
},
},
};
const resolved = { qDimensions: [] };
populate({ sn, properties: { a: { b: { c: resolved } } }, fields: ['=A'] }, context);
expect(h.addMeasure).to.have.been.calledWithExactly('=A');
});

it("should identify field as measure when prefixed with '='", () => {
expect(ft('=a')).to.equal('measure');
});

it('should identify field as measure when object looks like NxMeasure', () => {
expect(ft({ qDef: { qDef: {} } })).to.equal('measure');
});

it("should identify field as measure when object contains a qLibraryId and type==='measure'", () => {
expect(ft({ qLibraryId: 'a', type: 'measure' })).to.equal('measure');
});
});
Loading

0 comments on commit 0f5cb2e

Please sign in to comment.