From 073703ef5b8495944767c35cb3846655e4309048 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 12 Aug 2018 08:20:35 +0200 Subject: [PATCH] Created Home component tests --- package.json | 3 +- src/common/Home.js | 7 ++++- test/home/Home.test.js | 52 +++++++++++++++++++++++++++++++++ yarn.lock | 66 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 test/home/Home.test.js diff --git a/package.json b/package.json index bd8f411a..ca67eded 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node scripts/start.js", "build": "node scripts/build.js", - "test": "node scripts/test.js --env=jsdom" + "test": "node scripts/test.js --env=jsdom --colors" }, "dependencies": { "@fortawesome/fontawesome": "^1.1.8", @@ -70,6 +70,7 @@ "react-dev-utils": "^5.0.1", "resolve": "1.6.0", "sass-loader": "^7.0.1", + "sinon": "^6.1.5", "style-loader": "0.19.0", "sw-precache-webpack-plugin": "0.11.4", "url-loader": "0.6.2", diff --git a/src/common/Home.js b/src/common/Home.js index 3455cda4..8efcd317 100644 --- a/src/common/Home.js +++ b/src/common/Home.js @@ -28,7 +28,12 @@ export class Home extends React.Component { {hasServers && ( {servers.map(({ name, id }) => ( - + {name} diff --git a/test/home/Home.test.js b/test/home/Home.test.js new file mode 100644 index 00000000..52af9f97 --- /dev/null +++ b/test/home/Home.test.js @@ -0,0 +1,52 @@ +import { shallow } from 'enzyme' +import { values } from 'ramda' +import React from 'react' +import * as sinon from 'sinon' +import { Home } from '../../src/common/Home' + +describe('', () => { + let wrapped; + const defaultProps = { + resetSelectedServer: () => {}, + servers: {}, + }; + const createComponent = props => { + const actualProps = { ...defaultProps, ...props }; + wrapped = shallow(); + return wrapped; + }; + + afterEach(() => { + if (wrapped !== undefined) { + wrapped.unmount(); + wrapped = undefined; + } + }); + + it('resets selected server when mounted', () => { + const resetSelectedServer = sinon.spy(); + + expect(resetSelectedServer.called).toEqual(false); + createComponent({ resetSelectedServer }); + expect(resetSelectedServer.called).toEqual(true); + }); + + it('shows link to create server when no servers exist', () => { + const wrapped = createComponent(); + + expect(wrapped.find('Link')).toHaveLength(1); + expect(wrapped.find('ListGroup')).toHaveLength(0); + }); + + it('shows servers list when list of servers is not empty', () => { + const servers = { + 1: { name: 'foo', id: '123' }, + 2: { name: 'bar', id: '456' }, + } + const wrapped = createComponent({ servers }); + + expect(wrapped.find('Link')).toHaveLength(0); + expect(wrapped.find('ListGroup')).toHaveLength(1); + expect(wrapped.find('ListGroupItem')).toHaveLength(values(servers).length); + }); +}); diff --git a/yarn.lock b/yarn.lock index 8e47e890..7c4c492c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -31,6 +31,22 @@ humps "^2.0.1" prop-types "^15.5.7" +"@sinonjs/commons@^1.0.1": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.0.2.tgz#3e0ac737781627b8844257fadc3d803997d0526e" + dependencies: + type-detect "4.0.8" + +"@sinonjs/formatio@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-2.0.0.tgz#84db7e9eb5531df18a8c5e0bfb6e449e55e654b2" + dependencies: + samsam "1.3.0" + +"@sinonjs/samsam@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-2.0.0.tgz#9163742ac35c12d3602dece74317643b35db6a80" + "@types/node@*": version "10.5.6" resolved "https://registry.yarnpkg.com/@types/node/-/node-10.5.6.tgz#1640f021dd0eaf12e731e54198c12ad2e020dc8e" @@ -2151,7 +2167,7 @@ detect-port-alt@1.1.6: address "^1.0.1" debug "^2.6.0" -diff@^3.2.0: +diff@^3.2.0, diff@^3.5.0: version "3.5.0" resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" @@ -4451,6 +4467,10 @@ jsx-ast-utils@^2.0.0: dependencies: array-includes "^3.0.3" +just-extend@^1.1.27: + version "1.1.27" + resolved "https://registry.yarnpkg.com/just-extend/-/just-extend-1.1.27.tgz#ec6e79410ff914e472652abfa0e603c03d60e905" + killable@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/killable/-/killable-1.0.0.tgz#da8b84bd47de5395878f95d64d02f2449fe05e6b" @@ -4598,6 +4618,10 @@ lodash.flattendeep@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2" +lodash.get@^4.4.2: + version "4.4.2" + resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99" + lodash.isfunction@^3.0.9: version "3.0.9" resolved "https://registry.yarnpkg.com/lodash.isfunction/-/lodash.isfunction-3.0.9.tgz#06de25df4db327ac931981d1bdb067e5af68d051" @@ -4647,6 +4671,10 @@ loglevel@^1.4.1: version "1.6.1" resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.1.tgz#e0fc95133b6ef276cdc8887cdaf24aa6f156f8fa" +lolex@^2.3.2, lolex@^2.7.1: + version "2.7.1" + resolved "https://registry.yarnpkg.com/lolex/-/lolex-2.7.1.tgz#e40a8c4d1f14b536aa03e42a537c7adbaf0c20be" + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -4975,6 +5003,16 @@ next-tick@1: version "1.0.0" resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c" +nise@^1.4.2: + version "1.4.2" + resolved "https://registry.yarnpkg.com/nise/-/nise-1.4.2.tgz#a9a3800e3994994af9e452333d549d60f72b8e8c" + dependencies: + "@sinonjs/formatio" "^2.0.0" + just-extend "^1.1.27" + lolex "^2.3.2" + path-to-regexp "^1.7.0" + text-encoding "^0.6.4" + no-case@^2.2.0: version "2.3.2" resolved "https://registry.yarnpkg.com/no-case/-/no-case-2.3.2.tgz#60b813396be39b3f1288a4c1ed5d1e7d28b464ac" @@ -6623,6 +6661,10 @@ safe-regex@^1.1.0: version "2.1.2" resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" +samsam@1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.3.0.tgz#8d1d9350e25622da30de3e44ba692b5221ab7c50" + sane@~1.6.0: version "1.6.0" resolved "https://registry.yarnpkg.com/sane/-/sane-1.6.0.tgz#9610c452307a135d29c1fdfe2547034180c46775" @@ -6819,6 +6861,20 @@ signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" +sinon@^6.1.5: + version "6.1.5" + resolved "https://registry.yarnpkg.com/sinon/-/sinon-6.1.5.tgz#41451502d43cd5ffb9d051fbf507952400e81d09" + dependencies: + "@sinonjs/commons" "^1.0.1" + "@sinonjs/formatio" "^2.0.0" + "@sinonjs/samsam" "^2.0.0" + diff "^3.5.0" + lodash.get "^4.4.2" + lolex "^2.7.1" + nise "^1.4.2" + supports-color "^5.4.0" + type-detect "^4.0.8" + slash@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55" @@ -7237,6 +7293,10 @@ test-exclude@^4.2.1: read-pkg-up "^1.0.1" require-main-filename "^1.0.1" +text-encoding@^0.6.4: + version "0.6.4" + resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19" + text-table@0.2.0, text-table@~0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" @@ -7366,6 +7426,10 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" +type-detect@4.0.8, type-detect@^4.0.8: + version "4.0.8" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c" + type-is@~1.6.15, type-is@~1.6.16: version "1.6.16" resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz#f89ce341541c672b25ee7ae3c73dee3b2be50194"