From 4e7868c2cccc2dc259959b5a96c3bcf178d7b5d2 Mon Sep 17 00:00:00 2001 From: unix Date: Fri, 24 Apr 2020 14:53:57 +0800 Subject: [PATCH] test(snippet): add testcase --- .../__snapshots__/index.test.tsx.snap | 337 ++++++++++++++++++ components/snippet/__tests__/index.test.tsx | 79 ++++ 2 files changed, 416 insertions(+) create mode 100644 components/snippet/__tests__/__snapshots__/index.test.tsx.snap create mode 100644 components/snippet/__tests__/index.test.tsx diff --git a/components/snippet/__tests__/__snapshots__/index.test.tsx.snap b/components/snippet/__tests__/__snapshots__/index.test.tsx.snap new file mode 100644 index 0000000..0ad7846 --- /dev/null +++ b/components/snippet/__tests__/__snapshots__/index.test.tsx.snap @@ -0,0 +1,337 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Snippet should render correctly 1`] = ` +"
yarn add @zeit-ui/react
" +`; + +exports[`Snippet should work with different styles 1`] = ` +"
yarn add @zeit-ui/react
yarn add @zeit-ui/react
yarn add @zeit-ui/react
yarn add @zeit-ui/react
" +`; + +exports[`Snippet should work with multi-line 1`] = ` +"
cd project
now
" +`; diff --git a/components/snippet/__tests__/index.test.tsx b/components/snippet/__tests__/index.test.tsx new file mode 100644 index 0000000..5c5fc44 --- /dev/null +++ b/components/snippet/__tests__/index.test.tsx @@ -0,0 +1,79 @@ +import React from 'react' +import { mount } from 'enzyme' +import { Snippet } from 'components' + +const command = 'yarn add @zeit-ui/react' +const multiLine = ['cd project', 'now'] + +describe('Snippet', () => { + beforeAll(() => { + window.getSelection = jest.fn() + .mockImplementation(() => ({ + removeAllRanges: jest.fn(), + addRange: jest.fn(), + })) + document.createRange = jest.fn() + .mockImplementation(() => ({ + selectNode: jest.fn(), + })) + }) + + it('should render correctly', () => { + const wrapper = mount() + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + + it('should work with multi-line', () => { + const wrapper = mount() + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + + it('should work with different styles', () => { + const wrapper = mount( +
+ + + + +
+ ) + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + + it('text should be copied', () => { + document.execCommand = jest.fn() + const wrapper = mount() + wrapper.find('.copy').simulate('click') + expect(document.execCommand).toHaveBeenCalled() + ;(document.execCommand as jest.Mock).mockRestore() + }) + + it('multi-line commands should be copied', () => { + document.execCommand = jest.fn() + const wrapper = mount() + wrapper.find('.copy').simulate('click') + expect(document.execCommand).toHaveBeenCalled() + ;(document.execCommand as jest.Mock).mockRestore() + }) + + it('child commands should be copied', () => { + document.execCommand = jest.fn() + const wrapper = mount({command}) + wrapper.find('.copy').simulate('click') + expect(document.execCommand).toHaveBeenCalled() + ;(document.execCommand as jest.Mock).mockRestore() + }) + + it('should disable copy function', () => { + const wrapper = mount() + expect(wrapper.find('.copy').length).toBe(0) + }) + + afterAll(() => { + (window.getSelection as jest.Mock).mockRestore() + ;(document.createRange as jest.Mock).mockRestore() + }) +})