Browse Source

Add a couple more unit tests for the utils folder

pull/4219/head
John Cowen 7 years ago
parent
commit
950a704b74
  1. 46
      ui-v2/tests/unit/utils/get-component-factory-test.js
  2. 19
      ui-v2/tests/unit/utils/hasStatus-test.js
  3. 32
      ui-v2/tests/unit/utils/qsa-factory-test.js
  4. 93
      ui-v2/tests/unit/utils/sumOfUnhealthy-test.js

46
ui-v2/tests/unit/utils/get-component-factory-test.js

@ -3,8 +3,46 @@ import { module, test } from 'qunit';
module('Unit | Utility | get component factory');
// Replace this with your real tests.
test('it works', function(assert) {
let result = getComponentFactory({ lookup: function() {} });
assert.ok(result);
test("it uses lookup to locate the instance of the component based on the DOM element's id", function(assert) {
const expected = 'name';
let getComponent = getComponentFactory({
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);
});

19
ui-v2/tests/unit/utils/hasStatus-test.js

@ -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');

32
ui-v2/tests/unit/utils/qsa-factory-test.js

@ -3,8 +3,32 @@ import { module, test } from 'qunit';
module('Unit | Utility | qsa factory');
// Replace this with your real tests.
test('it works', function(assert) {
let result = qsaFactory();
assert.ok(result);
test('querySelectorAll is called on `document` when called with document', function(assert) {
assert.expect(2);
const expected = 'html';
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));
});

93
ui-v2/tests/unit/utils/sumOfUnhealthy-test.js

@ -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');
Loading…
Cancel
Save