Skip to content

Commit

Permalink
improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Dec 1, 2016
1 parent 11971a6 commit f6463b8
Showing 1 changed file with 83 additions and 158 deletions.
241 changes: 83 additions & 158 deletions src/core_plugins/tagcloud/public/__tests__/tag_cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import expect from 'expect.js';
import _ from 'lodash';
import TagCloud from 'plugins/tagcloud/tag_cloud';
import d3 from 'd3';
import {fromNode, delay} from 'bluebird';

describe('tag cloud tests', function () {

Expand Down Expand Up @@ -102,22 +103,15 @@ describe('tag cloud tests', function () {

describe(`should position elements correctly for options: ${JSON.stringify(test.options)}`, function () {

beforeEach(function () {
beforeEach(async function () {
setupDOM();
return new Promise((resolve, reject)=> {
tagCloud = new TagCloud(domNode);
tagCloud.setData(test.data);
tagCloud.setOptions(test.options);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});
});
tagCloud = new TagCloud(domNode);
tagCloud.setData(test.data);
tagCloud.setOptions(test.options);
await fromNode(cb => tagCloud.once('renderComplete', cb));
});

afterEach(function () {
teardownDOM();
});
afterEach(teardownDOM);

it('completeness should be ok', handleExpectedBlip(function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.COMPLETE);
Expand All @@ -142,33 +136,24 @@ describe('tag cloud tests', function () {
].forEach(function (timeout) {
describe(`should only send single renderComplete event at the very end, using ${timeout}ms timeout`, function () {

beforeEach(function () {
beforeEach(async function () {
setupDOM();
return new Promise((resolve, reject)=> {

//TagCloud takes at least 600ms to complete (due to d3 animation)
//renderComplete should only notify at the last one
tagCloud = new TagCloud(domNode);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);

//this timeout modifies the settings before the cloud is rendered.
//the cloud needs to use the correct options
setTimeout(() => {
tagCloud.setOptions(logScaleTest.options);
}, timeout);

});
});
//TagCloud takes at least 600ms to complete (due to d3 animation)
//renderComplete should only notify at the last one
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);

//this timeout modifies the settings before the cloud is rendered.
//the cloud needs to use the correct options
setTimeout(() => tagCloud.setOptions(logScaleTest.options), timeout);
await fromNode(cb => tagCloud.once('renderComplete', cb));

afterEach(function () {
teardownDOM();
});

afterEach(teardownDOM);

it('completeness should be ok', handleExpectedBlip(function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.COMPLETE);
}));
Expand All @@ -178,30 +163,21 @@ describe('tag cloud tests', function () {
verifyTagProperties(logScaleTest.expected, textElements, tagCloud);
}));


});
});

describe('should use the latest state before notifying (when modifying options multiple times)', function () {

beforeEach(function () {
beforeEach(async function () {
setupDOM();
return new Promise((resolve, reject)=> {
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
tagCloud.setOptions(logScaleTest.options);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});
});
});

afterEach(function () {
teardownDOM();
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
tagCloud.setOptions(logScaleTest.options);
await fromNode(cb => tagCloud.once('renderComplete', cb));
});

afterEach(teardownDOM);

it('completeness should be ok', handleExpectedBlip(function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.COMPLETE);
Expand All @@ -215,24 +191,16 @@ describe('tag cloud tests', function () {

describe('should use the latest state before notifying (when modifying data multiple times)', function () {

beforeEach(function () {
return new Promise((resolve, reject)=> {
setupDOM();
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
tagCloud.setData(trimDataTest.data);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});
});
});

afterEach(function () {
teardownDOM();
beforeEach(async function () {
setupDOM();
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
tagCloud.setData(trimDataTest.data);
await fromNode(cb => tagCloud.once('renderComplete', cb));
});

afterEach(teardownDOM);

it('completeness should be ok', handleExpectedBlip(function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.COMPLETE);
Expand Down Expand Up @@ -278,53 +246,40 @@ describe('tag cloud tests', function () {
});
});

afterEach(function () {
teardownDOM();
});
afterEach(teardownDOM);

it('completeness should be ok', handleExpectedBlip(function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.COMPLETE);
}));
it('positions should be ok', function () {
it('positions should be ok', handleExpectedBlip(function () {
const textElements = domNode.querySelectorAll('text');
verifyTagProperties(logScaleTest.expected, textElements, tagCloud);
});
}));

});


describe('should show correct data when state-updates are interleaved with resize event', function () {

beforeEach(function () {
beforeEach(async function () {
setupDOM();
return new Promise((resolve, reject) => {
tagCloud = new TagCloud(domNode);
tagCloud.setData(logScaleTest.data);
tagCloud.setOptions(logScaleTest.options);


setTimeout(() => {//make the container bigger during layout
domNode.style.width = '600px';
domNode.style.height = '600px';
tagCloud.resize();
setTimeout(() => {//change the options at the very end too
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
}, 200);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});

}, 1000);//let layout run
});
tagCloud = new TagCloud(domNode);
tagCloud.setData(logScaleTest.data);
tagCloud.setOptions(logScaleTest.options);

await delay(1000);//let layout run
domNode.style.width = '600px';
domNode.style.height = '600px';
tagCloud.resize();//triggers new layout
setTimeout(() => {//change the options at the very end too
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
}, 200);
await fromNode(cb => tagCloud.once('renderComplete', cb));

});


afterEach(function () {
teardownDOM();
});
afterEach(teardownDOM);

it('completeness should be ok', handleExpectedBlip(function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.COMPLETE);
Expand All @@ -340,25 +295,17 @@ describe('tag cloud tests', function () {

describe(`should not put elements in view when container is too small`, function () {

beforeEach(function () {
beforeEach(async function () {
setupDOM();
domNode.style.width = '1px';
domNode.style.height = '1px';
return new Promise((resolve, reject)=> {
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});
});

tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
await fromNode(cb => tagCloud.once('renderComplete', cb));
});

afterEach(function () {
teardownDOM();
});
afterEach(teardownDOM);

it('completeness should not be ok', function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.INCOMPLETE);
Expand All @@ -375,36 +322,25 @@ describe('tag cloud tests', function () {

describe(`tags should fit after making container bigger`, function () {

beforeEach(function () {
beforeEach(async function () {
setupDOM();
domNode.style.width = '1px';
domNode.style.height = '1px';
return new Promise((resolve, reject)=> {
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);

//make bigger
domNode.style.width = '512px';
domNode.style.height = '512px';
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
await fromNode(cb => tagCloud.once('renderComplete', cb));

tagCloud.on('renderComplete', _ => {
console.log('ready');
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});
tagCloud.resize();

});
});
//make bigger
domNode.style.width = '512px';
domNode.style.height = '512px';
tagCloud.resize();
await fromNode(cb => tagCloud.once('renderComplete', cb));

});

afterEach(function () {
teardownDOM();
});
afterEach(teardownDOM);

it('completeness should be ok', handleExpectedBlip(function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.COMPLETE);
Expand All @@ -414,32 +350,22 @@ describe('tag cloud tests', function () {

describe(`tags should no longer fit after making container smaller`, function () {

beforeEach(function () {
beforeEach(async function () {
setupDOM();
return new Promise((resolve, reject)=> {
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
tagCloud.on('renderComplete', function onRender() {
tagCloud.removeListener('renderComplete', onRender);
tagCloud = new TagCloud(domNode);
tagCloud.setData(baseTest.data);
tagCloud.setOptions(baseTest.options);
await fromNode(cb => tagCloud.once('renderComplete', cb));

//make smaller
domNode.style.width = '1px';
domNode.style.height = '1px';

tagCloud.on('renderComplete', _ => {
tagCloud.removeListener('renderComplete', onRender);
resolve(true);
});
tagCloud.resize();
//make smaller
domNode.style.width = '1px';
domNode.style.height = '1px';
tagCloud.resize();
await fromNode(cb => tagCloud.once('renderComplete', cb));

});
});
});

afterEach(function () {
teardownDOM();
});
afterEach(teardownDOM);

it('completeness should not be ok', function () {
expect(tagCloud.getStatus()).to.equal(TagCloud.STATUS.INCOMPLETE);
Expand Down Expand Up @@ -496,7 +422,6 @@ describe('tag cloud tests', function () {
}
}


/**
* In CI, this entire suite "blips" about 1/5 times.
* This blip causes the majority of these tests fail for the exact same reason: One tag is centered inside the container,
Expand All @@ -519,7 +444,7 @@ describe('tag cloud tests', function () {
const centered = (largest[1] === 0 && largest[2] === 0);
const inside = debugInfo.positions.filter(position => {
return debugInfo.size[0] <= position[1] && position[1] <= debugInfo.size[0]
&& debugInfo.size[1] <= position[2] || position[2] <= debugInfo.size[1];
&& debugInfo.size[1] <= position[2] && position[2] <= debugInfo.size[1];
});

return centered && inside.length === count - 1;
Expand All @@ -529,7 +454,7 @@ describe('tag cloud tests', function () {
function handleExpectedBlip(assertion) {
return function () {
if (!shouldAssert()) {
console.warn('Skipping tag cloud test');
console.warn('Skipping assertion.');
return;
}
assertion();
Expand Down

0 comments on commit f6463b8

Please sign in to comment.