From e702616f32227d77a5b9fb95ab530304368d8629 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 12 Mar 2020 16:41:33 -0400 Subject: [PATCH] example dispatching to store only --- cypress/integration/store-spec.js | 39 +++++++++++++++++++++++++++++++ src/index.js | 15 +++++------- src/store.js | 10 ++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 cypress/integration/store-spec.js create mode 100644 src/store.js diff --git a/cypress/integration/store-spec.js b/cypress/integration/store-spec.js new file mode 100644 index 0000000..5d987ec --- /dev/null +++ b/cypress/integration/store-spec.js @@ -0,0 +1,39 @@ +/// +import {completeAllTodos} from '../../src/actions' +describe('data store', () => { + beforeEach(() => { + cy.visit('/', { + onBeforeLoad (win) { + win.skipRender = true + } + }) + }) + + it('adds todos', () => { + cy.window().its('store').invoke('dispatch', { + type: 'ADD_TODO', + text: 'My todo' + }) + + cy.window().its('store').invoke('dispatch', { + type: 'ADD_TODO', + text: 'second todo' + }) + + cy.window().its('store').invoke('dispatch', { + type: 'ADD_TODO', + text: 'third todo' + }) + + cy.window().its('store').invoke('getState') + .its('todos') + .should('have.length', 3) + + cy.window().its('store').invoke('dispatch', completeAllTodos()) + + cy.window().its('store').invoke('getState') + .its('todos').should('deep.equal', [{id: 0, completed: true, text: "My todo"}, + {id: 1, completed: true, text: "second todo"}, + {id: 2, completed: true, text: "third todo"},]) + }) +}) diff --git a/src/index.js b/src/index.js index 669c81b..a7acf71 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,15 @@ import React from 'react' import { render } from 'react-dom' import { Provider } from 'react-redux' -import { createStore } from 'redux' import 'todomvc-app-css/index.css' import App from './components/App' -import reducer from './reducers' +import {store} from './store' -const store = createStore(reducer) +if (window.Cypress) { + if (window.skipRender) { + return + } +} render( @@ -14,9 +17,3 @@ render( , document.getElementById('root') ) - -// expose store during tests -/* istanbul ignore else */ -if (window.Cypress) { - window.store = store -} diff --git a/src/store.js b/src/store.js new file mode 100644 index 0000000..ef1ce4e --- /dev/null +++ b/src/store.js @@ -0,0 +1,10 @@ +import { createStore } from 'redux' +import reducer from './reducers' + +export const store = createStore(reducer) + +// expose store during tests +/* istanbul ignore else */ +if (window.Cypress) { + window.store = store +}