From 83af48112c88e48168c7e718529bc7dc88b74854 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 26 Mar 2018 21:55:46 +0300 Subject: [PATCH] tests: add standalone component test --- src/__tests__/ssr.test.tsx | 2 +- src/__tests__/standalone.test.tsx | 21 +++++++++++ src/components/index.ts | 1 + src/services/__tests__/MarkerService.test.ts | 38 ++++++++++---------- 4 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 src/__tests__/standalone.test.tsx diff --git a/src/__tests__/ssr.test.tsx b/src/__tests__/ssr.test.tsx index 8c7b8a36..d7c6dfd8 100644 --- a/src/__tests__/ssr.test.tsx +++ b/src/__tests__/ssr.test.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { renderToString } from 'react-dom/server'; import { Redoc, createStore } from '../'; -var yaml = require('yaml-js'); +import * as yaml from 'yaml-js'; import { readFileSync } from 'fs'; import { resolve } from 'path'; diff --git a/src/__tests__/standalone.test.tsx b/src/__tests__/standalone.test.tsx new file mode 100644 index 00000000..31f5aee8 --- /dev/null +++ b/src/__tests__/standalone.test.tsx @@ -0,0 +1,21 @@ +import * as React from 'react'; +import { mount } from 'enzyme'; +import * as yaml from 'yaml-js'; + +import { readFileSync } from 'fs'; +import { resolve } from 'path'; + +// import { filterPropsDeep } from '../../../utils/test-utils'; + +import { RedocStandalone, Loading, StoreProvider, ErrorBoundary } from '../components/'; + +describe('Components', () => { + describe('RedocStandalone', () => { + test('should show loading first', () => { + const spec = yaml.load(readFileSync(resolve(__dirname, '../../demo/openapi.yaml'))); + + let inst = mount(); + expect(inst.find(Loading)).toHaveLength(1); + }); + }); +}); diff --git a/src/components/index.ts b/src/components/index.ts index 733fb8e8..2d1f9347 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -4,6 +4,7 @@ export * from './Redoc/Redoc'; export * from './Schema/'; export * from './SearchBox/SearchBox'; export * from './Operation/Operation'; +export * from './Loading/Loading'; export * from './RedocStandalone'; export * from './ErrorBoundary'; diff --git a/src/services/__tests__/MarkerService.test.ts b/src/services/__tests__/MarkerService.test.ts index 95b8168f..651ad2ec 100644 --- a/src/services/__tests__/MarkerService.test.ts +++ b/src/services/__tests__/MarkerService.test.ts @@ -1,12 +1,10 @@ -const markSpy = jest.fn(); -const unmarkSpy = jest.fn(); +const mockMark = jest.fn(); +const mockUnmark = jest.fn(); -class FakeMark { - mark = markSpy; - unmark = unmarkSpy; -} - -jest.mock('mark.js', () => FakeMark); +jest.mock('mark.js', () => () => ({ + mark: mockMark, + unmark: mockUnmark, +})); import { MarkerService } from '../MarkerService'; @@ -16,8 +14,8 @@ describe('Marker service', () => { beforeEach(() => { marker = new MarkerService(); - markSpy.mockClear(); - unmarkSpy.mockClear(); + mockMark.mockClear(); + mockUnmark.mockClear(); }); test('add element to Map', () => { @@ -42,7 +40,7 @@ describe('Marker service', () => { marker.addOnly([element, e2, e3]); - expect(unmarkSpy).toHaveBeenCalledTimes(1); + expect(mockUnmark).toHaveBeenCalledTimes(1); expect(marker.map.size).toEqual(3); }); @@ -55,8 +53,8 @@ describe('Marker service', () => { marker.unmark(); - expect(unmarkSpy).toHaveBeenCalledTimes(3); - expect(markSpy).not.toHaveBeenCalled(); + expect(mockUnmark).toHaveBeenCalledTimes(3); + expect(mockMark).not.toHaveBeenCalled(); }); test('clearAll: should unmark and remove all elements', () => { const e1 = document.createElement('span'); @@ -67,8 +65,8 @@ describe('Marker service', () => { marker.clearAll(); - expect(unmarkSpy).toHaveBeenCalledTimes(3); - expect(markSpy).not.toHaveBeenCalled(); + expect(mockUnmark).toHaveBeenCalledTimes(3); + expect(mockMark).not.toHaveBeenCalled(); expect(marker.map.size).toEqual(0); }); @@ -81,9 +79,9 @@ describe('Marker service', () => { marker.mark('test'); - expect(unmarkSpy).toHaveBeenCalledTimes(3); - expect(markSpy).toHaveBeenCalledTimes(3); - expect(markSpy).toHaveBeenCalledWith('test'); + expect(mockUnmark).toHaveBeenCalledTimes(3); + expect(mockMark).toHaveBeenCalledTimes(3); + expect(mockMark).toHaveBeenCalledWith('test'); expect(marker.map.size).toEqual(3); }); @@ -91,13 +89,13 @@ describe('Marker service', () => { marker.add(element); marker.mark(); - expect(markSpy).not.toHaveBeenCalled(); + expect(mockMark).not.toHaveBeenCalled(); }); test('mark: should save previous marked term and use it if no term is provided', () => { marker.add(element); marker.mark('test'); marker.mark(); - expect(markSpy).toHaveBeenLastCalledWith('test'); + expect(mockMark).toHaveBeenLastCalledWith('test'); }); });