diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 760cf687..7ab4a744 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1356,6 +1356,12 @@ "integrity": "sha512-1OzrNb4RuAzIT7wHSsgZRlMBlNsJl+do6UblR7JMW4oB7bbR+uBEYtUh7gEc/jM84GGilh68lSOokyM/zNUlBA==", "dev": true }, + "@types/sinon": { + "version": "7.5.1", + "resolved": "https://registry.npmjs.org/@types/sinon/-/sinon-7.5.1.tgz", + "integrity": "sha512-EZQUP3hSZQyTQRfiLqelC9NMWd1kqLcmQE0dMiklxBkgi84T+cHOhnKpgk4NnOWpGX863yE6+IaGnOXUNFqDnQ==", + "dev": true + }, "@types/source-list-map": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/@types/source-list-map/-/source-list-map-0.1.2.tgz", diff --git a/package.json b/package.json index d851cbd8..30b6417a 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "@types/react-dom": "^16.8.4", "@types/request": "^2.48.4", "@types/semver": "^6.2.0", + "@types/sinon": "^7.5.1", "@types/tmp": "^0.1.0", "@types/webpack-node-externals": "^1.7.0", "babel-loader": "^8.0.4", diff --git a/tests/gui/os/window-progress.spec.js b/tests/gui/os/window-progress.spec.js deleted file mode 100644 index 9893a6fc..00000000 --- a/tests/gui/os/window-progress.spec.js +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2016 balena.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict' - -const m = require('mochainon') -// eslint-disable-next-line node/no-missing-require -const windowProgress = require('../../../lib/gui/app/os/window-progress') - -describe('Browser: WindowProgress', function () { - describe('windowProgress', function () { - describe('given a stubbed current window', function () { - beforeEach(function () { - this.setProgressBarSpy = m.sinon.spy() - this.setTitleSpy = m.sinon.spy() - - windowProgress.currentWindow = { - setProgressBar: this.setProgressBarSpy, - setTitle: this.setTitleSpy - } - - this.state = { - flashing: 1, - verifying: 0, - successful: 0, - failed: 0, - percentage: 85, - speed: 100 - } - }) - - describe('.set()', function () { - it('should translate 0-100 percentages to 0-1 ranges', function () { - windowProgress.set(this.state) - m.chai.expect(this.setProgressBarSpy).to.have.been.calledWith(0.85) - }) - - it('should set 0 given 0', function () { - this.state.percentage = 0 - windowProgress.set(this.state) - m.chai.expect(this.setProgressBarSpy).to.have.been.calledWith(0) - }) - - it('should set 1 given 100', function () { - this.state.percentage = 100 - windowProgress.set(this.state) - m.chai.expect(this.setProgressBarSpy).to.have.been.calledWith(1) - }) - - it('should throw if given a percentage higher than 100', function () { - this.state.percentage = 101 - const state = this.state - m.chai.expect(function () { - windowProgress.set(state) - }).to.throw('Invalid percentage: 101') - }) - - it('should throw if given a percentage less than 0', function () { - this.state.percentage = -1 - const state = this.state - m.chai.expect(function () { - windowProgress.set(state) - }).to.throw('Invalid percentage: -1') - }) - - it('should set the flashing title', function () { - windowProgress.set(this.state) - m.chai.expect(this.setTitleSpy).to.have.been.calledWith(' \u2013 85% Flashing') - }) - - it('should set the verifying title', function () { - this.state.flashing = 0 - this.state.verifying = 1 - windowProgress.set(this.state) - m.chai.expect(this.setTitleSpy).to.have.been.calledWith(' \u2013 85% Validating') - }) - - it('should set the starting title', function () { - this.state.percentage = 0 - this.state.speed = 0 - windowProgress.set(this.state) - m.chai.expect(this.setTitleSpy).to.have.been.calledWith(' \u2013 Starting...') - }) - - it('should set the finishing title', function () { - this.state.percentage = 100 - windowProgress.set(this.state) - m.chai.expect(this.setTitleSpy).to.have.been.calledWith(' \u2013 Finishing...') - }) - }) - - describe('.clear()', function () { - it('should set -1', function () { - windowProgress.clear() - m.chai.expect(this.setProgressBarSpy).to.have.been.calledWith(-1) - }) - - it('should clear the window title', function () { - windowProgress.clear() - m.chai.expect(this.setTitleSpy).to.have.been.calledWith('') - }) - }) - }) - }) -}) diff --git a/tests/gui/os/window-progress.spec.ts b/tests/gui/os/window-progress.spec.ts new file mode 100644 index 00000000..08df5854 --- /dev/null +++ b/tests/gui/os/window-progress.spec.ts @@ -0,0 +1,115 @@ +/* + * Copyright 2016 balena.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from 'chai'; +import { assert, spy } from 'sinon'; + +import * as windowProgress from '../../../lib/gui/app/os/window-progress'; + +describe('Browser: WindowProgress', function() { + describe('windowProgress', function() { + describe('given a stubbed current window', function() { + beforeEach(function() { + this.setProgressBarSpy = spy(); + this.setTitleSpy = spy(); + + windowProgress.currentWindow.setProgressBar = this.setProgressBarSpy; + windowProgress.currentWindow.setTitle = this.setTitleSpy; + + this.state = { + flashing: 1, + verifying: 0, + successful: 0, + failed: 0, + percentage: 85, + speed: 100, + }; + }); + + describe('.set()', function() { + it('should translate 0-100 percentages to 0-1 ranges', function() { + windowProgress.set(this.state); + assert.calledWith(this.setProgressBarSpy, 0.85); + }); + + it('should set 0 given 0', function() { + this.state.percentage = 0; + windowProgress.set(this.state); + assert.calledWith(this.setProgressBarSpy, 0); + }); + + it('should set 1 given 100', function() { + this.state.percentage = 100; + windowProgress.set(this.state); + assert.calledWith(this.setProgressBarSpy, 1); + }); + + it('should throw if given a percentage higher than 100', function() { + this.state.percentage = 101; + const state = this.state; + expect(function() { + windowProgress.set(state); + }).to.throw('Invalid percentage: 101'); + }); + + it('should throw if given a percentage less than 0', function() { + this.state.percentage = -1; + const state = this.state; + expect(function() { + windowProgress.set(state); + }).to.throw('Invalid percentage: -1'); + }); + + it('should set the flashing title', function() { + windowProgress.set(this.state); + assert.calledWith(this.setTitleSpy, ' – 85% Flashing'); + }); + + it('should set the verifying title', function() { + this.state.flashing = 0; + this.state.verifying = 1; + windowProgress.set(this.state); + assert.calledWith(this.setTitleSpy, ' – 85% Validating'); + }); + + it('should set the starting title', function() { + this.state.percentage = 0; + this.state.speed = 0; + windowProgress.set(this.state); + assert.calledWith(this.setTitleSpy, ' – Starting...'); + }); + + it('should set the finishing title', function() { + this.state.percentage = 100; + windowProgress.set(this.state); + assert.calledWith(this.setTitleSpy, ' – Finishing...'); + }); + }); + + describe('.clear()', function() { + it('should set -1', function() { + windowProgress.clear(); + assert.calledWith(this.setProgressBarSpy, -1); + }); + + it('should clear the window title', function() { + windowProgress.clear(); + assert.calledWith(this.setTitleSpy, ''); + }); + }); + }); + }); +});