From cf1f47e86ca78493e91a6b5dae888777cc830ba1 Mon Sep 17 00:00:00 2001 From: Griffithtp Date: Sat, 6 Jul 2019 17:43:00 +0100 Subject: [PATCH] chore: add unit test for component --- src/components/Dist/Dist.test.tsx | 80 +++++++++++++++++++ src/components/Dist/Dist.tsx | 4 +- .../Dist/__snapshots__/Dist.test.tsx.snap | 7 ++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/components/Dist/Dist.test.tsx create mode 100644 src/components/Dist/__snapshots__/Dist.test.tsx.snap diff --git a/src/components/Dist/Dist.test.tsx b/src/components/Dist/Dist.test.tsx new file mode 100644 index 0000000..5a3a10b --- /dev/null +++ b/src/components/Dist/Dist.test.tsx @@ -0,0 +1,80 @@ +import React from 'react'; +import { shallow } from 'enzyme'; + +describe(' component', () => { + beforeEach(() => { + jest.resetModules(); + }); + + test('should render the component in default state', () => { + const packageMeta = { + latest: { + name: 'verdaccio', + version: '4.0.0', + dist: { + fileCount: 7, + unpackedSize: 10, + }, + license: '', + }, + }; + jest.doMock('../../pages/version/Version', () => ({ + DetailContextConsumer: component => { + return component.children({ packageMeta }); + }, + })); + + const Dist = require('./Dist').default; + const wrapper = shallow(); + expect(wrapper.html()).toMatchSnapshot(); + }); + + test('should render the component with license as string', () => { + const packageMeta = { + latest: { + name: 'verdaccio', + version: '4.0.0', + dist: { + fileCount: 7, + unpackedSize: 10, + }, + license: 'MIT', + }, + }; + jest.doMock('../../pages/version/Version', () => ({ + DetailContextConsumer: component => { + return component.children({ packageMeta }); + }, + })); + + const Dist = require('./Dist').default; + const wrapper = shallow(); + expect(wrapper.html()).toMatchSnapshot(); + }); + + test('should render the component with license as object', () => { + const packageMeta = { + latest: { + name: 'verdaccio', + version: '4.0.0', + dist: { + fileCount: 7, + unpackedSize: 10, + }, + license: { + type: 'MIT', + url: 'https://www.opensource.org/licenses/mit-license.php', + }, + }, + }; + jest.doMock('../../pages/version/Version', () => ({ + DetailContextConsumer: component => { + return component.children({ packageMeta }); + }, + })); + + const Dist = require('./Dist').default; + const wrapper = shallow(); + expect(wrapper.html()).toMatchSnapshot(); + }); +}); diff --git a/src/components/Dist/Dist.tsx b/src/components/Dist/Dist.tsx index 088a085..2edcfee 100644 --- a/src/components/Dist/Dist.tsx +++ b/src/components/Dist/Dist.tsx @@ -31,10 +31,10 @@ class Dist extends Component { const value = dist === 'license' ? formatLicense(distDict[dist]) : distDict[dist]; const label = ( - + <> {/* eslint-disable-next-line */} {dist.replace('-', ' ')}: {value} - + ); return ; }); diff --git a/src/components/Dist/__snapshots__/Dist.test.tsx.snap b/src/components/Dist/__snapshots__/Dist.test.tsx.snap new file mode 100644 index 0000000..15ebd6c --- /dev/null +++ b/src/components/Dist/__snapshots__/Dist.test.tsx.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` component should render the component in default state 1`] = `"

    Latest Distribution

  • file count: 7
    size: 10.00 Bytes
"`; + +exports[` component should render the component with license as object 1`] = `"

    Latest Distribution

  • file count: 7
    size: 10.00 Bytes
    license: MIT
"`; + +exports[` component should render the component with license as string 1`] = `"

    Latest Distribution

  • file count: 7
    size: 10.00 Bytes
    license: MIT
"`;