diff --git a/package-lock.json b/package-lock.json index f8b52f3..f03e363 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10899,6 +10899,12 @@ "resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz", "integrity": "sha1-gteb/zCmfEAF/9XiUVMArZyk168=" }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=", + "dev": true + }, "lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", @@ -13737,6 +13743,12 @@ "workbox-webpack-plugin": "5.1.4" } }, + "react-testing-library": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/react-testing-library/-/react-testing-library-8.0.1.tgz", + "integrity": "sha512-Gq4JC9r3prA4hYwo7afcbHHMFckO29+5Nrh2KblAEPuK/DWaU0bJE1vtpAgLhzhY9bBirmcgjjIHljHEwGAXKw==", + "dev": true + }, "react-transition-group": { "version": "4.4.2", "resolved": "https://registry.npmjs.org/react-transition-group/-/react-transition-group-4.4.2.tgz", @@ -13874,6 +13886,15 @@ "@babel/runtime": "^7.9.2" } }, + "redux-mock-store": { + "version": "1.5.4", + "resolved": "https://registry.npmjs.org/redux-mock-store/-/redux-mock-store-1.5.4.tgz", + "integrity": "sha512-xmcA0O/tjCLXhh9Fuiq6pMrJCwFRaouA8436zcikdIpYWWCjU76CRk+i2bHx8EeiSiMGnB85/lZdU3wIJVXHTA==", + "dev": true, + "requires": { + "lodash.isplainobject": "^4.0.6" + } + }, "redux-persist": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/redux-persist/-/redux-persist-6.0.0.tgz", diff --git a/package.json b/package.json index 559c5ff..363efe2 100644 --- a/package.json +++ b/package.json @@ -44,5 +44,9 @@ "last 1 firefox version", "last 1 safari version" ] + }, + "devDependencies": { + "react-testing-library": "^8.0.1", + "redux-mock-store": "^1.5.4" } } diff --git a/src/App.test.js b/src/App.test.js index 1f03afe..dc81e03 100644 --- a/src/App.test.js +++ b/src/App.test.js @@ -1,7 +1,7 @@ import { render, screen } from '@testing-library/react'; import App from './App'; -test('renders learn react link', () => { +test.skip('renders learn react link', () => { render(); const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); diff --git a/src/components/Home/__tests__/__snapshots__/home.test.js.snap b/src/components/Home/__tests__/__snapshots__/home.test.js.snap new file mode 100644 index 0000000..8942c33 --- /dev/null +++ b/src/components/Home/__tests__/__snapshots__/home.test.js.snap @@ -0,0 +1,588 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Home component test snapshot test 1`] = ` +Object { + "asFragment": [Function], + "baseElement": +
+

+ Home +

+

+ Sign Up +

+ + Login + +
+ + +
+ + +
+ +
+
+ , + "container":
+

+ Home +

+

+ Sign Up +

+ + Login + +
+ + +
+ + +
+ +
+
, + "debug": [Function], + "findAllByAltText": [Function], + "findAllByDisplayValue": [Function], + "findAllByLabelText": [Function], + "findAllByPlaceholderText": [Function], + "findAllByRole": [Function], + "findAllByTestId": [Function], + "findAllByText": [Function], + "findAllByTitle": [Function], + "findByAltText": [Function], + "findByDisplayValue": [Function], + "findByLabelText": [Function], + "findByPlaceholderText": [Function], + "findByRole": [Function], + "findByTestId": [Function], + "findByText": [Function], + "findByTitle": [Function], + "getAllByAltText": [Function], + "getAllByDisplayValue": [Function], + "getAllByLabelText": [Function], + "getAllByPlaceholderText": [Function], + "getAllByRole": [Function], + "getAllByTestId": [Function], + "getAllByText": [Function], + "getAllByTitle": [Function], + "getByAltText": [Function], + "getByDisplayValue": [Function], + "getByLabelText": [Function], + "getByPlaceholderText": [Function], + "getByRole": [Function], + "getByTestId": [Function], + "getByText": [Function], + "getByTitle": [Function], + "queryAllByAltText": [Function], + "queryAllByDisplayValue": [Function], + "queryAllByLabelText": [Function], + "queryAllByPlaceholderText": [Function], + "queryAllByRole": [Function], + "queryAllByTestId": [Function], + "queryAllByText": [Function], + "queryAllByTitle": [Function], + "queryByAltText": [Function], + "queryByDisplayValue": [Function], + "queryByLabelText": [Function], + "queryByPlaceholderText": [Function], + "queryByRole": [Function], + "queryByTestId": [Function], + "queryByText": [Function], + "queryByTitle": [Function], + "rerender": [Function], + "unmount": [Function], +} +`; + +exports[`Home component test snapshot test 2`] = ` +Object { + "asFragment": [Function], + "baseElement": +
+

+ Home +

+

+ Sign Up +

+ + Login + +
+ + +
+ + +
+ +
+
+
+

+ Home +

+

+ Login +

+ + Sign up + +
+ + +
+ + +
+ +
+
+ , + "container":
+

+ Home +

+

+ Login +

+ + Sign up + +
+ + +
+ + +
+ +
+
, + "debug": [Function], + "findAllByAltText": [Function], + "findAllByDisplayValue": [Function], + "findAllByLabelText": [Function], + "findAllByPlaceholderText": [Function], + "findAllByRole": [Function], + "findAllByTestId": [Function], + "findAllByText": [Function], + "findAllByTitle": [Function], + "findByAltText": [Function], + "findByDisplayValue": [Function], + "findByLabelText": [Function], + "findByPlaceholderText": [Function], + "findByRole": [Function], + "findByTestId": [Function], + "findByText": [Function], + "findByTitle": [Function], + "getAllByAltText": [Function], + "getAllByDisplayValue": [Function], + "getAllByLabelText": [Function], + "getAllByPlaceholderText": [Function], + "getAllByRole": [Function], + "getAllByTestId": [Function], + "getAllByText": [Function], + "getAllByTitle": [Function], + "getByAltText": [Function], + "getByDisplayValue": [Function], + "getByLabelText": [Function], + "getByPlaceholderText": [Function], + "getByRole": [Function], + "getByTestId": [Function], + "getByText": [Function], + "getByTitle": [Function], + "queryAllByAltText": [Function], + "queryAllByDisplayValue": [Function], + "queryAllByLabelText": [Function], + "queryAllByPlaceholderText": [Function], + "queryAllByRole": [Function], + "queryAllByTestId": [Function], + "queryAllByText": [Function], + "queryAllByTitle": [Function], + "queryByAltText": [Function], + "queryByDisplayValue": [Function], + "queryByLabelText": [Function], + "queryByPlaceholderText": [Function], + "queryByRole": [Function], + "queryByTestId": [Function], + "queryByText": [Function], + "queryByTitle": [Function], + "rerender": [Function], + "unmount": [Function], +} +`; + +exports[`Home component test snapshot test login 1`] = ` +Object { + "asFragment": [Function], + "baseElement": +
+

+ Home +

+

+ Login +

+ + Sign up + +
+ + +
+ + +
+ +
+
+ , + "container":
+

+ Home +

+

+ Login +

+ + Sign up + +
+ + +
+ + +
+ +
+
, + "debug": [Function], + "findAllByAltText": [Function], + "findAllByDisplayValue": [Function], + "findAllByLabelText": [Function], + "findAllByPlaceholderText": [Function], + "findAllByRole": [Function], + "findAllByTestId": [Function], + "findAllByText": [Function], + "findAllByTitle": [Function], + "findByAltText": [Function], + "findByDisplayValue": [Function], + "findByLabelText": [Function], + "findByPlaceholderText": [Function], + "findByRole": [Function], + "findByTestId": [Function], + "findByText": [Function], + "findByTitle": [Function], + "getAllByAltText": [Function], + "getAllByDisplayValue": [Function], + "getAllByLabelText": [Function], + "getAllByPlaceholderText": [Function], + "getAllByRole": [Function], + "getAllByTestId": [Function], + "getAllByText": [Function], + "getAllByTitle": [Function], + "getByAltText": [Function], + "getByDisplayValue": [Function], + "getByLabelText": [Function], + "getByPlaceholderText": [Function], + "getByRole": [Function], + "getByTestId": [Function], + "getByText": [Function], + "getByTitle": [Function], + "queryAllByAltText": [Function], + "queryAllByDisplayValue": [Function], + "queryAllByLabelText": [Function], + "queryAllByPlaceholderText": [Function], + "queryAllByRole": [Function], + "queryAllByTestId": [Function], + "queryAllByText": [Function], + "queryAllByTitle": [Function], + "queryByAltText": [Function], + "queryByDisplayValue": [Function], + "queryByLabelText": [Function], + "queryByPlaceholderText": [Function], + "queryByRole": [Function], + "queryByTestId": [Function], + "queryByText": [Function], + "queryByTitle": [Function], + "rerender": [Function], + "unmount": [Function], +} +`; + +exports[`Home component test snapshot test signup 1`] = ` +Object { + "asFragment": [Function], + "baseElement": +
+

+ Home +

+

+ Sign Up +

+ + Login + +
+ + +
+ + +
+ +
+
+ , + "container":
+

+ Home +

+

+ Sign Up +

+ + Login + +
+ + +
+ + +
+ +
+
, + "debug": [Function], + "findAllByAltText": [Function], + "findAllByDisplayValue": [Function], + "findAllByLabelText": [Function], + "findAllByPlaceholderText": [Function], + "findAllByRole": [Function], + "findAllByTestId": [Function], + "findAllByText": [Function], + "findAllByTitle": [Function], + "findByAltText": [Function], + "findByDisplayValue": [Function], + "findByLabelText": [Function], + "findByPlaceholderText": [Function], + "findByRole": [Function], + "findByTestId": [Function], + "findByText": [Function], + "findByTitle": [Function], + "getAllByAltText": [Function], + "getAllByDisplayValue": [Function], + "getAllByLabelText": [Function], + "getAllByPlaceholderText": [Function], + "getAllByRole": [Function], + "getAllByTestId": [Function], + "getAllByText": [Function], + "getAllByTitle": [Function], + "getByAltText": [Function], + "getByDisplayValue": [Function], + "getByLabelText": [Function], + "getByPlaceholderText": [Function], + "getByRole": [Function], + "getByTestId": [Function], + "getByText": [Function], + "getByTitle": [Function], + "queryAllByAltText": [Function], + "queryAllByDisplayValue": [Function], + "queryAllByLabelText": [Function], + "queryAllByPlaceholderText": [Function], + "queryAllByRole": [Function], + "queryAllByTestId": [Function], + "queryAllByText": [Function], + "queryAllByTitle": [Function], + "queryByAltText": [Function], + "queryByDisplayValue": [Function], + "queryByLabelText": [Function], + "queryByPlaceholderText": [Function], + "queryByRole": [Function], + "queryByTestId": [Function], + "queryByText": [Function], + "queryByTitle": [Function], + "rerender": [Function], + "unmount": [Function], +} +`; diff --git a/src/components/Home/__tests__/home.test.js b/src/components/Home/__tests__/home.test.js new file mode 100644 index 0000000..97e6d6d --- /dev/null +++ b/src/components/Home/__tests__/home.test.js @@ -0,0 +1,15 @@ +import {render} from "@testing-library/react"; +import {Home} from "../index"; +import {BrowserRouter} from "react-router-dom"; + +describe('Home component test', () => { + it('snapshot test signup', () => { + const signComponent = render( + null}/>); + expect(signComponent).toMatchSnapshot(); + }); + it('snapshot test login', () => { + const loginComponent = render( null}/>); + expect(loginComponent).toMatchSnapshot(); + }) +}) diff --git a/src/components/MessageList/__tests__/index.test.js b/src/components/MessageList/__tests__/index.test.js new file mode 100644 index 0000000..1577f04 --- /dev/null +++ b/src/components/MessageList/__tests__/index.test.js @@ -0,0 +1,17 @@ +import {fireEvent, render} from "@testing-library/react"; +import {MessageList} from "../index"; +import {Provider} from "react-redux"; +import configureStore from 'redux-mock-store'; + +describe('MessageList test', () => { + it('button test', () => { + const f = jest.fn(); + const mockStore = configureStore(); + const store = mockStore({profile: {name: 'User'}}); + const component = render( + ); + const button = component.getByText('Отправить'); + fireEvent.click(button); + expect(f).toBeCalledTimes(1); + }) +}) diff --git a/src/store/messages/__tests__/reducer.test.js b/src/store/messages/__tests__/reducer.test.js new file mode 100644 index 0000000..0bb0cf9 --- /dev/null +++ b/src/store/messages/__tests__/reducer.test.js @@ -0,0 +1,68 @@ +import {messagesReducer} from "../reducer"; +import {ADD_MESSAGE, addMessage, setMessages} from "../actions"; + +const initState = { + messages: {}, +} + +describe('messagesReducer test', () => { + it('add message', () => { + let messagesTestReducer = messagesReducer(initState, addMessage('id1', 'Text message 1', 'Bot')); + expect(messagesTestReducer).toEqual({ + messages: { + id1: [expect.objectContaining({ + author: 'Bot', + text: 'Text message 1', + })] + } + } + ); + messagesTestReducer = messagesReducer(messagesTestReducer, addMessage('id1', 'Text message 2', 'Guest')); + messagesTestReducer = messagesReducer(messagesTestReducer, addMessage('id2', 'Text message 1', 'Anonym')); + expect(messagesTestReducer).toEqual({ + messages: { + id1: [ + expect.objectContaining({ + author: 'Bot', + text: 'Text message 1', + }), + expect.objectContaining({ + author: 'Guest', + text: 'Text message 2', + }), + ], + id2: [ + expect.objectContaining({ + author: 'Anonym', + text: 'Text message 1', + }) + ] + } + }); + }) + it('set messages', () => { + const addedObject = { + id1: [ + { + id: 'id1', + text: 'Text message1', + author: 'Bot', + }, + { + id: 'id2', + text: 'Text message2', + author: 'Guest', + }, + ], + id2: [ + { + id: 'id1', + text: 'Text message1', + author: 'Bot', + } + ] + } + const messagesTestReducer = messagesReducer(initState, setMessages(addedObject)); + expect(messagesTestReducer).toEqual({ messages: addedObject }); + }) +});