From 5bda6a062e03958954ffa8e625fb8f448837f7cc Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Mon, 12 Aug 2019 15:13:35 +0100 Subject: [PATCH] Add tests for colour preview --- tests/javascripts/colourPreview.test.js | 139 ++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 tests/javascripts/colourPreview.test.js diff --git a/tests/javascripts/colourPreview.test.js b/tests/javascripts/colourPreview.test.js new file mode 100644 index 000000000..92a511781 --- /dev/null +++ b/tests/javascripts/colourPreview.test.js @@ -0,0 +1,139 @@ +const helpers = require('./support/helpers.js'); + +beforeAll(() => { + require('../../app/assets/javascripts/colourPreview.js'); +}); + +afterAll(() => { + require('./support/teardown.js'); +}); + +describe('Colour preview', () => { + + let field; + let textbox; + let swatchEl; + + beforeEach(() => { + + // set up DOM + document.body.innerHTML = ` +
+ + +
`; + + field = document.querySelector('.form-group'); + textbox = document.querySelector('input[type=text]'); + + }); + + afterEach(() => { + + document.body.innerHTML = ''; + + }); + + describe("When the page loads", () => { + + test("It should add a swatch element for the preview", () => { + + // start the module + window.GOVUK.modules.start(); + + swatchEl = document.querySelector('.textbox-colour-preview'); + + expect(swatchEl).not.toBeNull(); + + }); + + test("If the textbox is empty it should make the swatch white", () => { + + // start the module + window.GOVUK.modules.start(); + + swatchEl = document.querySelector('.textbox-colour-preview'); + + // textbox defaults to empty + // colours are output in RGB + expect(swatchEl.style.background).toEqual('rgb(255, 255, 255)'); + + }); + + test("If the textbox has a value which is a hex code it should add that colour to the swatch", () => { + + textbox.setAttribute('value', '#00FF00'); + + // start the module + window.GOVUK.modules.start(); + + swatchEl = document.querySelector('.textbox-colour-preview'); + + // colours are output in RGB + expect(swatchEl.style.background).toEqual('rgb(0, 255, 0)'); + + }); + + test("If the textbox has a value which isn't a hex code it should make the swatch white", () => { + + textbox.setAttribute('value', 'green'); + + // start the module + window.GOVUK.modules.start(); + + swatchEl = document.querySelector('.textbox-colour-preview'); + + // colours are output in RGB + expect(swatchEl.style.background).toEqual('rgb(255, 255, 255)'); + + }); + + }); + + describe("When input is added to the textbox", () => { + + beforeEach(() => { + + // start the module + window.GOVUK.modules.start(); + + swatchEl = document.querySelector('.textbox-colour-preview'); + + }); + + test("If the textbox is empty it should make the swatch white", () => { + + helpers.triggerEvent(document.querySelector('input[type=text]'), 'input'); + + // textbox defaults to empty + expect(swatchEl.style.background).toEqual('rgb(255, 255, 255)'); + + }); + + test("If the textbox has a value which is a hex code it should add that colour to the swatch", () => { + + textbox.setAttribute('value', '#00FF00'); + + helpers.triggerEvent(document.querySelector('input[type=text]'), 'input'); + + // textbox defaults to empty + expect(swatchEl.style.background).toEqual('rgb(0, 255, 0)'); + + }); + + test("If the textbox has a value which isn't a hex code it should make the swatch white", () => { + + textbox.setAttribute('value', 'green'); + + helpers.triggerEvent(document.querySelector('input[type=text]'), 'input'); + + // textbox defaults to empty + expect(swatchEl.style.background).toEqual('rgb(255, 255, 255)'); + + }); + + }); + +});