Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions superglue/spec/lib/flashReducer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { describe, expect, it } from 'vitest'
import { flashReducer } from '../../lib/reducers'

describe('flash reducer', () => {
describe('RESET', () => {
it('resets to empty', () => {
const prevState = { notice: 'hello' }
const nextState = flashReducer(prevState, {
type: '@@superglue/RESET',
})
expect(nextState).toEqual({})
})
})

describe('BEFORE_VISIT', () => {
it('clears flash on visit', () => {
const prevState = { notice: 'hello', alert: 'danger' }
const nextState = flashReducer(prevState, {
type: '@@superglue/BEFORE_VISIT',
payload: {
currentPageKey: '/foo',
fetchArgs: ['/foo', {}],
},
})
expect(nextState).toEqual({})
})
})

describe('CLEAR_FLASH', () => {
it('clears all flash when no key provided', () => {
const prevState = { notice: 'hello', alert: 'danger' }
const nextState = flashReducer(prevState, {
type: '@@superglue/CLEAR_FLASH',
payload: {},
})
expect(nextState).toEqual({})
})

it('clears a specific key', () => {
const prevState = { notice: 'hello', alert: 'danger' }
const nextState = flashReducer(prevState, {
type: '@@superglue/CLEAR_FLASH',
payload: { key: 'notice' },
})
expect(nextState).toEqual({ alert: 'danger' })
})
})

describe('FLASH', () => {
it('merges flash data', () => {
const prevState = { notice: 'hello' }
const nextState = flashReducer(prevState, {
type: '@@superglue/FLASH',
payload: { flash: { alert: 'danger' } },
})
expect(nextState).toEqual({ notice: 'hello', alert: 'danger' })
})

it('overwrites existing keys', () => {
const prevState = { notice: 'hello' }
const nextState = flashReducer(prevState, {
type: '@@superglue/FLASH',
payload: { flash: { notice: 'updated' } },
})
expect(nextState).toEqual({ notice: 'updated' })
})
})

describe('RECEIVE_RESPONSE', () => {
it('merges flash from response', () => {
const prevState = {}
const nextState = flashReducer(prevState, {
type: '@@superglue/RECEIVE_RESPONSE',
payload: {
pageKey: '/foo',
response: {
flash: { notice: 'Success!' },
},
},
})
expect(nextState).toEqual({ notice: 'Success!' })
})

it('returns state when response has no flash', () => {
const prevState = { notice: 'existing' }
const nextState = flashReducer(prevState, {
type: '@@superglue/RECEIVE_RESPONSE',
payload: {
pageKey: '/foo',
response: {},
},
})
expect(nextState).toEqual({ notice: 'existing' })
})
})
})
96 changes: 1 addition & 95 deletions superglue/spec/lib/reducers.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { pageReducer, superglueReducer, flashReducer } from '../../lib/reducers'
import { pageReducer, superglueReducer } from '../../lib/reducers'

describe('reducers', () => {
describe('superglue reducer', () => {
Expand Down Expand Up @@ -468,98 +468,4 @@ describe('reducers', () => {
})
})
})

describe('flash reducer', () => {
describe('RESET', () => {
it('resets to empty', () => {
const prevState = { notice: 'hello' }
const nextState = flashReducer(prevState, {
type: '@@superglue/RESET',
})
expect(nextState).toEqual({})
})
})

describe('BEFORE_VISIT', () => {
it('clears flash on visit', () => {
const prevState = { notice: 'hello', alert: 'danger' }
const nextState = flashReducer(prevState, {
type: '@@superglue/BEFORE_VISIT',
payload: {
currentPageKey: '/foo',
fetchArgs: ['/foo', {}],
},
})
expect(nextState).toEqual({})
})
})

describe('CLEAR_FLASH', () => {
it('clears all flash when no key provided', () => {
const prevState = { notice: 'hello', alert: 'danger' }
const nextState = flashReducer(prevState, {
type: '@@superglue/CLEAR_FLASH',
payload: {},
})
expect(nextState).toEqual({})
})

it('clears a specific key', () => {
const prevState = { notice: 'hello', alert: 'danger' }
const nextState = flashReducer(prevState, {
type: '@@superglue/CLEAR_FLASH',
payload: { key: 'notice' },
})
expect(nextState).toEqual({ alert: 'danger' })
})
})

describe('FLASH', () => {
it('merges flash data', () => {
const prevState = { notice: 'hello' }
const nextState = flashReducer(prevState, {
type: '@@superglue/FLASH',
payload: { flash: { alert: 'danger' } },
})
expect(nextState).toEqual({ notice: 'hello', alert: 'danger' })
})

it('overwrites existing keys', () => {
const prevState = { notice: 'hello' }
const nextState = flashReducer(prevState, {
type: '@@superglue/FLASH',
payload: { flash: { notice: 'updated' } },
})
expect(nextState).toEqual({ notice: 'updated' })
})
})

describe('RECEIVE_RESPONSE', () => {
it('merges flash from response', () => {
const prevState = {}
const nextState = flashReducer(prevState, {
type: '@@superglue/RECEIVE_RESPONSE',
payload: {
pageKey: '/foo',
response: {
flash: { notice: 'Success!' },
},
},
})
expect(nextState).toEqual({ notice: 'Success!' })
})

it('returns state when response has no flash', () => {
const prevState = { notice: 'existing' }
const nextState = flashReducer(prevState, {
type: '@@superglue/RECEIVE_RESPONSE',
payload: {
pageKey: '/foo',
response: {},
},
})
expect(nextState).toEqual({ notice: 'existing' })
})
})
})
})
Loading