From 950a704b746010b5ec83bf4762b5cec59843f5cb Mon Sep 17 00:00:00 2001 From: John Cowen Date: Mon, 4 Jun 2018 15:47:51 +0100 Subject: [PATCH] Add a couple more unit tests for the utils folder --- .../unit/utils/get-component-factory-test.js | 46 ++++++++- ui-v2/tests/unit/utils/hasStatus-test.js | 19 ++++ ui-v2/tests/unit/utils/qsa-factory-test.js | 32 ++++++- ui-v2/tests/unit/utils/sumOfUnhealthy-test.js | 93 +++++++++++++++++++ 4 files changed, 182 insertions(+), 8 deletions(-) create mode 100644 ui-v2/tests/unit/utils/hasStatus-test.js create mode 100644 ui-v2/tests/unit/utils/sumOfUnhealthy-test.js diff --git a/ui-v2/tests/unit/utils/get-component-factory-test.js b/ui-v2/tests/unit/utils/get-component-factory-test.js index ad39e3ae23..16be5c677e 100644 --- a/ui-v2/tests/unit/utils/get-component-factory-test.js +++ b/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); }); diff --git a/ui-v2/tests/unit/utils/hasStatus-test.js b/ui-v2/tests/unit/utils/hasStatus-test.js new file mode 100644 index 0000000000..6ddfe37a1d --- /dev/null +++ b/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'); diff --git a/ui-v2/tests/unit/utils/qsa-factory-test.js b/ui-v2/tests/unit/utils/qsa-factory-test.js index 76ed1ea144..feadc8c3f5 100644 --- a/ui-v2/tests/unit/utils/qsa-factory-test.js +++ b/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)); }); diff --git a/ui-v2/tests/unit/utils/sumOfUnhealthy-test.js b/ui-v2/tests/unit/utils/sumOfUnhealthy-test.js new file mode 100644 index 0000000000..a8766db048 --- /dev/null +++ b/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');