From 09bcb1a812837b1e0e6e0056599051c3013acc0c Mon Sep 17 00:00:00 2001 From: meriadec Date: Tue, 6 Mar 2018 15:05:02 +0100 Subject: [PATCH] Add snapshot tests from FormattedVal --- .eslintrc | 2 + flow-defs/globals.js | 1 + jest.config.js | 6 ++ package.json | 1 + .../__tests__/FormattedVal.test.js | 45 +++++++++++++++ .../__snapshots__/FormattedVal.test.js.snap | 55 +++++++++++++++++++ .../index.js} | 0 src/helpers/staticPath.js | 5 +- src/test-utils.js | 9 +++ yarn.lock | 8 +++ 10 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 jest.config.js create mode 100644 src/components/base/FormattedVal/__tests__/FormattedVal.test.js create mode 100644 src/components/base/FormattedVal/__tests__/__snapshots__/FormattedVal.test.js.snap rename src/components/base/{FormattedVal.js => FormattedVal/index.js} (100%) create mode 100644 src/test-utils.js diff --git a/.eslintrc b/.eslintrc index 0167e955..176ff361 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,6 +5,7 @@ "globals": { "__ENV__": false, "__DEV__": false, + "__TEST__": false, "__PROD__": false, "__SENTRY_URL__": false, "__PRINT_MODE__": false, @@ -17,6 +18,7 @@ "jest": false, "describe": false, "test": false, + "it": false, "expect": false, }, "rules": { diff --git a/flow-defs/globals.js b/flow-defs/globals.js index e4b4c70c..db928b57 100644 --- a/flow-defs/globals.js +++ b/flow-defs/globals.js @@ -1,6 +1,7 @@ /* eslint-disable */ declare var __DEV__: boolean +declare var __TEST__: boolean declare var __PROD__: boolean declare var __ENV__: string declare var __PRINT_MODE__: string diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 00000000..022217f5 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + globals: { + __DEV__: true, + __TEST__: true, + }, +} diff --git a/package.json b/package.json index dfb8b8ae..f085da15 100644 --- a/package.json +++ b/package.json @@ -141,6 +141,7 @@ "node-loader": "^0.6.0", "prettier": "^1.11.1", "react-hot-loader": "^4.0.0", + "react-test-renderer": "^16.2.0", "webpack": "^3.11.0" } } diff --git a/src/components/base/FormattedVal/__tests__/FormattedVal.test.js b/src/components/base/FormattedVal/__tests__/FormattedVal.test.js new file mode 100644 index 00000000..40e219d0 --- /dev/null +++ b/src/components/base/FormattedVal/__tests__/FormattedVal.test.js @@ -0,0 +1,45 @@ +import React from 'react' +import { getDefaultUnitByCoinType } from '@ledgerhq/currencies' + +import { render } from 'test-utils' +import FormattedVal from '..' + +const bitcoinUnit = getDefaultUnitByCoinType(0) + +describe('components', () => { + describe('FormattedVal', () => { + it('renders a formatted val', () => { + const component = + const tree = render(component) + expect(tree).toMatchSnapshot() + }) + + it('shows sign', () => { + const component = + const tree = render(component) + expect(tree).toMatchSnapshot() + + const component2 = + const tree2 = render(component2) + expect(tree2).toMatchSnapshot() + }) + + it('shows code', () => { + const component = + const tree = render(component) + expect(tree).toMatchSnapshot() + }) + + it('renders a percent', () => { + const component = + const tree = render(component) + expect(tree).toMatchSnapshot() + }) + + it('renders a fiat', () => { + const component = + const tree = render(component) + expect(tree).toMatchSnapshot() + }) + }) +}) diff --git a/src/components/base/FormattedVal/__tests__/__snapshots__/FormattedVal.test.js.snap b/src/components/base/FormattedVal/__tests__/__snapshots__/FormattedVal.test.js.snap new file mode 100644 index 00000000..4dc2e4de --- /dev/null +++ b/src/components/base/FormattedVal/__tests__/__snapshots__/FormattedVal.test.js.snap @@ -0,0 +1,55 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`components FormattedVal renders a fiat 1`] = ` + + 20.00 + +`; + +exports[`components FormattedVal renders a formatted val 1`] = ` + + 4 + +`; + +exports[`components FormattedVal renders a percent 1`] = ` + + 30 % + +`; + +exports[`components FormattedVal shows code 1`] = ` + + BTC 4 + +`; + +exports[`components FormattedVal shows sign 1`] = ` + + + 4 + +`; + +exports[`components FormattedVal shows sign 2`] = ` + + - 4 + +`; diff --git a/src/components/base/FormattedVal.js b/src/components/base/FormattedVal/index.js similarity index 100% rename from src/components/base/FormattedVal.js rename to src/components/base/FormattedVal/index.js diff --git a/src/helpers/staticPath.js b/src/helpers/staticPath.js index 6146b80a..6bd7f9b6 100644 --- a/src/helpers/staticPath.js +++ b/src/helpers/staticPath.js @@ -1,7 +1,8 @@ const isStorybook = process.env.STORYBOOK_ENV -const isRunningInAsar = !isStorybook && process.mainModule.filename.indexOf('app.asar') !== -1 +const isRunningInAsar = + !isStorybook && process.mainModule && process.mainModule.filename.indexOf('app.asar') !== -1 -export default (__DEV__ && !isStorybook +export default (__DEV__ && !isStorybook && !__TEST__ ? __static : isRunningInAsar ? __dirname.replace(/app\.asar$/, 'static') diff --git a/src/test-utils.js b/src/test-utils.js new file mode 100644 index 00000000..3288c903 --- /dev/null +++ b/src/test-utils.js @@ -0,0 +1,9 @@ +import React from 'react' +import renderer from 'react-test-renderer' +import { ThemeProvider } from 'styled-components' + +import theme from 'styles/theme' + +export function render(component) { + return renderer.create({component}).toJSON() +} diff --git a/yarn.lock b/yarn.lock index 7904b325..600edc2a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8309,6 +8309,14 @@ react-style-proptype@^3.0.0: dependencies: prop-types "^15.5.4" +react-test-renderer@^16.2.0: + version "16.2.0" + resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.2.0.tgz#bddf259a6b8fcd8555f012afc8eacc238872a211" + dependencies: + fbjs "^0.8.16" + object-assign "^4.1.1" + prop-types "^15.6.0" + react-textarea-autosize@^5.2.1: version "5.2.1" resolved "https://registry.yarnpkg.com/react-textarea-autosize/-/react-textarea-autosize-5.2.1.tgz#2b78f9067180f41b08ac59f78f1581abadd61e54"