Skip to content

Commit

Permalink
example dispatching to store only
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Mar 12, 2020
1 parent 03ebf08 commit e702616
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
39 changes: 39 additions & 0 deletions cypress/integration/store-spec.js
@@ -0,0 +1,39 @@
/// <reference types="cypress" />
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"},])
})
})
15 changes: 6 additions & 9 deletions src/index.js
@@ -1,22 +1,19 @@
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(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)

// expose store during tests
/* istanbul ignore else */
if (window.Cypress) {
window.store = store
}
10 changes: 10 additions & 0 deletions 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
}

0 comments on commit e702616

Please sign in to comment.