From 602ab1a687b2f61853f667056ff33c04f881a921 Mon Sep 17 00:00:00 2001 From: Matthew Shao Date: Thu, 8 Jun 2017 11:13:59 +0800 Subject: [PATCH] [web] Add tests for js/components/ContentView/ContentView.jsx --- .../components/ContentView/ContentViewSpec.js | 85 +++++++++++++++++++ .../__snapshots__/ContentViewSpec.js.snap | 52 ++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 web/src/js/__tests__/components/ContentView/ContentViewSpec.js create mode 100644 web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewSpec.js.snap diff --git a/web/src/js/__tests__/components/ContentView/ContentViewSpec.js b/web/src/js/__tests__/components/ContentView/ContentViewSpec.js new file mode 100644 index 000000000..f519185dd --- /dev/null +++ b/web/src/js/__tests__/components/ContentView/ContentViewSpec.js @@ -0,0 +1,85 @@ +import React from 'react' +import renderer from 'react-test-renderer' +import TestUtils from 'react-dom/test-utils' +import { Provider } from 'react-redux' +import { ViewServer, ViewImage, PureViewServer, Edit } from '../../../components/ContentView/ContentViews' +import { TFlow, TStore } from '../../ducks/tutils' +import mockXMLHttpRequest from 'mock-xmlhttprequest' + +global.XMLHttpRequest = mockXMLHttpRequest +let tflow = new TFlow() + +describe('ViewImage Component', () => { + let viewImage = renderer.create(), + tree = viewImage.toJSON() + + it('should render correctly', () => { + expect(tree).toMatchSnapshot() + }) +}) + +describe('ViewServer Component', () => { + let store = TStore(), + setContentViewDescFn = jest.fn(), + setContentFn = jest.fn() + + it('should render correctly and connect to state', () => { + let provider = renderer.create( + + + ), + tree = provider.toJSON() + expect(tree).toMatchSnapshot() + + let viewServer = renderer.create( + + ) + tree = viewServer.toJSON() + expect(tree).toMatchSnapshot() + }) + + it('should handle componentWillReceiveProps', () => { + // case of fail to parse content + let viewSever = TestUtils.renderIntoDocument( + + ) + viewSever.componentWillReceiveProps({...viewSever.props, content: '{foo' }) + let e = '' + try {JSON.parse('{foo') } catch(err){ e = err.message} + expect(viewSever.data).toEqual({ description: e, lines: [] }) + }) +}) + +describe('Edit Component', () => { + it('should render correctly', () => { + let edit = renderer.create(), + tree = edit.toJSON() + expect(tree).toMatchSnapshot() + }) +}) diff --git a/web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewSpec.js.snap b/web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewSpec.js.snap new file mode 100644 index 000000000..a4bc06c5a --- /dev/null +++ b/web/src/js/__tests__/components/ContentView/__snapshots__/ContentViewSpec.js.snap @@ -0,0 +1,52 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Edit Component should render correctly 1`] = ` +
+ +
+`; + +exports[`ViewImage Component should render correctly 1`] = ` +
+ preview +
+`; + +exports[`ViewServer Component should render correctly and connect to state 1`] = ` +
+ +
+`; + +exports[`ViewServer Component should render correctly and connect to state 2`] = ` +
+
+    
+ + 1 + + + 1 + +
+
+
+`;