Add a couple more unit tests for the utils folder

This commit is contained in:
John Cowen 2018-06-04 15:47:51 +01:00
parent e92fce188a
commit d3651097ea
4 changed files with 182 additions and 8 deletions

View File

@ -3,8 +3,46 @@ import { module, test } from 'qunit';
module('Unit | Utility | get component factory'); module('Unit | Utility | get component factory');
// Replace this with your real tests. test("it uses lookup to locate the instance of the component based on the DOM element's id", function(assert) {
test('it works', function(assert) { const expected = 'name';
let result = getComponentFactory({ lookup: function() {} }); let getComponent = getComponentFactory({
assert.ok(result); lookup: function() {
return { id: expected };
},
});
assert.equal(typeof getComponent, 'function', 'returns a function');
const actual = getComponent({
getAttribute: function(name) {
return 'id';
},
});
assert.equal(actual, expected, 'performs a lookup based on the id');
});
test("it returns null if it can't find it", function(assert) {
const expected = null;
let getComponent = getComponentFactory({
lookup: function() {
return { id: '' };
},
});
const actual = getComponent({
getAttribute: function(name) {
return 'non-existent';
},
});
assert.equal(actual, expected);
});
test('it returns null if there is no id', function(assert) {
const expected = null;
let getComponent = getComponentFactory({
lookup: function() {
return { id: '' };
},
});
const actual = getComponent({
getAttribute: function(name) {
return;
},
});
assert.equal(actual, expected);
}); });

View File

@ -0,0 +1,19 @@
import hasStatus from 'consul-ui/utils/hasStatus';
import { module, test, skip } from 'qunit';
module('Unit | Utility | has status');
const checks = {
filterBy: function(prop, value) {
return { length: 0 };
},
};
test('it returns true when passing an empty string (therefore "all")', function(assert) {
assert.ok(hasStatus(checks, ''));
});
test('it returns false when passing an actual status', function(assert) {
['passing', 'critical', 'warning'].forEach(function(item) {
assert.ok(!hasStatus(checks, item), `, with ${item}`);
});
});
skip('it works as a factory, passing ember `get` in to create the function');

View File

@ -3,8 +3,32 @@ import { module, test } from 'qunit';
module('Unit | Utility | qsa factory'); module('Unit | Utility | qsa factory');
// Replace this with your real tests. test('querySelectorAll is called on `document` when called with document', function(assert) {
test('it works', function(assert) { assert.expect(2);
let result = qsaFactory(); const expected = 'html';
assert.ok(result); const $$ = qsaFactory({
querySelectorAll: function(sel) {
assert.equal(sel, expected);
return true;
},
});
assert.ok($$(expected));
});
test('querySelectorAll is called on `context` when called with context', function(assert) {
assert.expect(2);
const expected = 'html';
const context = {
querySelectorAll: function(sel) {
assert.equal(sel, expected);
return true;
},
};
const $$ = qsaFactory({
// this should never be called
querySelectorAll: function(sel) {
assert.equal(sel, expected);
return false;
},
});
assert.ok($$(expected, context));
}); });

View File

@ -0,0 +1,93 @@
import sumOfUnhealthy from 'consul-ui/utils/sumOfUnhealthy';
import { module, test, skip } from 'qunit';
module('Unit | Utility | sum of unhealthy');
test('it returns the correct single count', function(assert) {
const expected = 1;
[
[
{
Status: 'critical',
},
],
[
{
Status: 'warning',
},
],
].forEach(function(checks) {
const actual = sumOfUnhealthy(checks);
assert.equal(actual, expected);
});
});
test('it returns the correct single count when there are none', function(assert) {
const expected = 0;
[
[
{
Status: 'passing',
},
{
Status: 'passing',
},
{
Status: 'passing',
},
{
Status: 'passing',
},
],
[
{
Status: 'passing',
},
],
].forEach(function(checks) {
const actual = sumOfUnhealthy(checks);
assert.equal(actual, expected);
});
});
test('it returns the correct multiple count', function(assert) {
const expected = 3;
[
[
{
Status: 'critical',
},
{
Status: 'warning',
},
{
Status: 'warning',
},
{
Status: 'passing',
},
],
[
{
Status: 'passing',
},
{
Status: 'critical',
},
{
Status: 'passing',
},
{
Status: 'warning',
},
{
Status: 'warning',
},
{
Status: 'passing',
},
],
].forEach(function(checks) {
const actual = sumOfUnhealthy(checks);
assert.equal(actual, expected);
});
});
skip('it works as a factory, passing ember `get` in to create the function');