Skip to content

Commit

Permalink
Merge pull request #2507 from LD4P/t2485-reducers
Browse files Browse the repository at this point in the history
Refactor reducers
  • Loading branch information
jcoyne authored Sep 21, 2020
2 parents 7e2476d + 77714a4 commit f4d481d
Show file tree
Hide file tree
Showing 35 changed files with 381 additions and 387 deletions.
5 changes: 1 addition & 4 deletions __tests__/actionCreators/resources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,7 @@ describe('saveNewResource', () => {
expect(actions).toHaveAction('SAVE_RESOURCE_FINISHED')

const saveResourceFinishedAction = actions.find((action) => action.type === 'SAVE_RESOURCE_FINISHED')
expect(safeAction(saveResourceFinishedAction)).toEqual({
type: 'SAVE_RESOURCE_FINISHED',
payload: 't9zVwg2zO',
})
expect(saveResourceFinishedAction.payload.resourceKey).toEqual('t9zVwg2zO')
})

it('error when saving a new resource', async () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/components/search/QASearchResults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('<QASearchResults />', () => {

it('renders errors', () => {
const state = createState()
state.selectorReducer.editor.errors.searchqaresource = ['Ooops']
state.editor.errors.searchqaresource = ['Ooops']
state.selectorReducer.search.results = [
{
uri: 'http://share-vde.org/sharevde/rdfBibframe/Work/3107365',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/components/search/SinopiaSearchResults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('<SinopiaSearchResults />', () => {
}]
state.selectorReducer.search.totalResults = 1
state.selectorReducer.search.query = 'twain'
state.selectorReducer.editor.errors.searchresource = ['Ooops']
state.editor.errors.searchresource = ['Ooops']

const store = createStore(state)
renderComponent(<SinopiaSearchResults />, store)
Expand Down
56 changes: 21 additions & 35 deletions __tests__/reducers/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const reducer = createReducer(handlers)
describe('addError()', () => {
it('adds new error without existing errors', () => {
const oldState = {
editor: {
errors: {},
},
errors: {},
}

const action = {
Expand All @@ -32,17 +30,15 @@ describe('addError()', () => {
}

const newState = reducer(oldState, action)
expect(newState.editor.errors).toStrictEqual({
expect(newState.errors).toStrictEqual({
rty6789: ['Failed to add a resource'],
})
})

it('adds error to existing errors', () => {
const oldState = {
editor: {
errors: {
er345v2: ['Existing validation error'],
},
errors: {
er345v2: ['Existing validation error'],
},
}

Expand All @@ -55,7 +51,7 @@ describe('addError()', () => {
}

const newState = reducer(oldState, action)
expect(newState.editor.errors.er345v2).toStrictEqual([
expect(newState.errors.er345v2).toStrictEqual([
'Existing validation error',
'Second validation error',
])
Expand All @@ -65,13 +61,11 @@ describe('addError()', () => {
describe('clearErrors()', () => {
it('sets errors to empty for a given errorKey', () => {
const oldState = {
editor: {
errors: {
gh345690: [
'a short error',
'a longer error message',
],
},
errors: {
gh345690: [
'a short error',
'a longer error message',
],
},
}

Expand All @@ -82,19 +76,15 @@ describe('clearErrors()', () => {

const newState = reducer(oldState, action)

expect(newState.editor.errors.gh345690).toStrictEqual([])
expect(newState.errors.gh345690).toStrictEqual([])
})
})

describe('hideValidationErrors()', () => {
it('sets show validation error for a key to false', () => {
const oldState = {
editor: {
resourceValidation: {
show: {
u230f67: true,
},
},
resourceValidation: {
u230f67: true,
},
}

Expand All @@ -105,22 +95,18 @@ describe('hideValidationErrors()', () => {

const newState = reducer(oldState, action)

expect(newState.editor.resourceValidation.show.u230f67).toBeFalsy()
expect(newState.resourceValidation.u230f67).toBeFalsy()
})
})

describe('showValidationErrors()', () => {
it('shows validation errors for a resource', () => {
const oldState = {
editor: {
modal: {
name: 'An error modal',
},
resourceValidation: {
show: {
fgen0234: false,
},
},
modal: {
name: 'An error modal',
},
resourceValidation: {
fgen0234: false,
},
}

Expand All @@ -131,7 +117,7 @@ describe('showValidationErrors()', () => {

const newState = reducer(oldState, action)

expect(newState.editor.modal.name).toBe(undefined)
expect(newState.editor.resourceValidation.show.fgen0234).toBeTruthy()
expect(newState.modal.name).toBe(null)
expect(newState.resourceValidation.fgen0234).toBeTruthy()
})
})
6 changes: 2 additions & 4 deletions __tests__/reducers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ describe('setAppVersion()', () => {
const action = { type: 'SET_APP_VERSION', payload: '2000.0.1' }

const oldState = {
appVersion: {
version: '3.0.0',
},
version: '3.0.0',
}

const newState = reducer(oldState, action)

expect(newState.appVersion.version).toStrictEqual('2000.0.1')
expect(newState.version).toStrictEqual('2000.0.1')
})
})

Expand Down
30 changes: 12 additions & 18 deletions __tests__/reducers/inputs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
import { createReducer } from 'reducers/index'

const handlers = {
HIDE_DIACRITICS: hideDiacriticsSelection,
SET_LITERAL_CONTENT: setLiteralInputContent,
HIDE_DIACRITICS: hideDiacriticsSelection,
SHOW_DIACRITICS: showDiacriticsSelection,
}

Expand All @@ -16,9 +16,7 @@ const reducer = createReducer(handlers)
describe('setLiteralInputContent()', () => {
it('sets a literal value', () => {
const oldState = {
editor: {
content: {},
},
content: {},
}

const action = {
Expand All @@ -30,7 +28,7 @@ describe('setLiteralInputContent()', () => {
}

const newState = reducer(oldState, action)
expect(newState.editor.content).toStrictEqual({
expect(newState.content).toStrictEqual({
'345adfe': 'A good thing',
})
})
Expand All @@ -39,11 +37,9 @@ describe('setLiteralInputContent()', () => {
describe('hideDiacriticsSelection()', () => {
it('hides the diacritic component', () => {
const oldState = {
editor: {
diacritics: {
show: true,
key: '3456abc',
},
diacritics: {
show: true,
key: '3456abc',
},
}

Expand All @@ -52,19 +48,17 @@ describe('hideDiacriticsSelection()', () => {
}

const newState = reducer(oldState, action)
expect(newState.editor.diacritics.show).toBeFalsy()
expect(newState.editor.diacritics.key).toBe(null)
expect(newState.diacritics.show).toBeFalsy()
expect(newState.diacritics.key).toBe(null)
})
})

describe('showDiacriticsSelection()', () => {
it('shows diacritic component', () => {
const oldState = {
editor: {
diacritics: {
show: false,
key: null,
},
diacritics: {
show: false,
key: null,
},
}

Expand All @@ -74,7 +68,7 @@ describe('showDiacriticsSelection()', () => {
}

const newState = reducer(oldState, action)
expect(newState.editor.diacritics).toStrictEqual({
expect(newState.diacritics).toStrictEqual({
show: true,
key: 'efq3450',
})
Expand Down
46 changes: 8 additions & 38 deletions __tests__/reducers/messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,24 @@ import { createReducer } from 'reducers/index'
describe('showCopyNewMessage()', () => {
const handlers = { SHOW_COPY_NEW_MESSAGE: showCopyNewMessage }
const reducer = createReducer(handlers)
const realDateNow = Date.now.bind(global.Date)

beforeAll(() => {
const dateNowStub = jest.fn(() => 1594667068562)
global.Date.now = dateNowStub
})

afterAll(() => {
global.Date.now = realDateNow
})

it('copies new message when payload has an URI', () => {
const oldState = {
editor: {
copyToNewMessage: {},
},
copyToNewMessage: {},
}
const action = {
type: 'SHOW_COPY_NEW_MESSAGE',
payload: 'https://sinopia.io/stanford/1234',
}

const newState = reducer(oldState, action)
expect(newState).toStrictEqual({
editor: {
copyToNewMessage: {
timestamp: 1594667068562,
oldUri: 'https://sinopia.io/stanford/1234',
},
},
})
})

it('copies new message when payload is absent', () => {
const oldState = {
editor: {
copyToNewMessage: {},
payload: {
oldUri: 'https://sinopia.io/stanford/1234',
timestamp: 1594667068562,
},
}
const action = {
type: 'SHOW_COPY_NEW_MESSAGE',
}

const newState = reducer(oldState, action)
expect(newState).toStrictEqual({
editor: {
copyToNewMessage: {
timestamp: 1594667068562,
},
copyToNewMessage: {
timestamp: 1594667068562,
oldUri: 'https://sinopia.io/stanford/1234',
},
})
})
Expand Down
Loading

0 comments on commit f4d481d

Please sign in to comment.