fix: dispatch ADD_START, correlate preview/lookup responses with the current attempt
This commit is contained in:
@@ -46,7 +46,7 @@ function stateOf(phase: ScanState['phase']): ScanState {
|
||||
{ type: 'DETECT', code: '5021592210629' },
|
||||
{ type: 'CANDIDATES', code: '5021592210629', candidates: [candidate] },
|
||||
{ type: 'SELECT', candidate },
|
||||
{ type: 'PREVIEW', preview },
|
||||
{ type: 'PREVIEW', preview, candidateId: 1001 },
|
||||
{ type: 'ADD_START' },
|
||||
{ type: 'ADDED', item },
|
||||
]
|
||||
@@ -83,10 +83,37 @@ describe('scanReducer', () => {
|
||||
})
|
||||
|
||||
it('ERROR maps kinds', () => {
|
||||
const next = scanReducer(stateOf('looking'), { type: 'ERROR', kind: 'no_discogs_token', code: '123' })
|
||||
const next = scanReducer(stateOf('looking'), {
|
||||
type: 'ERROR',
|
||||
kind: 'no_discogs_token',
|
||||
code: '123',
|
||||
source: 'lookup',
|
||||
})
|
||||
expect(next).toEqual({ phase: 'error', kind: 'no_discogs_token', code: '123' })
|
||||
})
|
||||
|
||||
it('ignores a stale preview for a different candidate', () => {
|
||||
const confirm = stateOf('confirm')
|
||||
const stale = scanReducer(confirm, { type: 'PREVIEW', preview, candidateId: 999 })
|
||||
expect(stale).toBe(confirm)
|
||||
})
|
||||
|
||||
it('ignores a preview-source error in the looking phase', () => {
|
||||
const looking = stateOf('looking')
|
||||
const next = scanReducer(looking, {
|
||||
type: 'ERROR', kind: 'server', code: null, source: 'preview', candidateId: 1001,
|
||||
})
|
||||
expect(next).toBe(looking)
|
||||
})
|
||||
|
||||
it('ignores a lookup-source error after the user moved past looking', () => {
|
||||
const candidates = stateOf('candidates')
|
||||
const next = scanReducer(candidates, {
|
||||
type: 'ERROR', kind: 'server', code: '123', source: 'lookup',
|
||||
})
|
||||
expect(next).toBe(candidates)
|
||||
})
|
||||
|
||||
it('SELECT from candidates → confirm with candidate, no preview yet', () => {
|
||||
const next = scanReducer(stateOf('candidates'), { type: 'SELECT', candidate })
|
||||
expect(next.phase).toBe('confirm')
|
||||
@@ -99,7 +126,7 @@ describe('scanReducer', () => {
|
||||
})
|
||||
|
||||
it('PREVIEW fills the confirm phase', () => {
|
||||
const next = scanReducer(stateOf('confirm'), { type: 'PREVIEW', preview })
|
||||
const next = scanReducer(stateOf('confirm'), { type: 'PREVIEW', preview, candidateId: 1001 })
|
||||
if (next.phase === 'confirm') expect(next.preview).toBe(preview)
|
||||
else throw new Error('expected confirm')
|
||||
})
|
||||
@@ -114,7 +141,7 @@ describe('scanReducer', () => {
|
||||
})
|
||||
|
||||
it('ADD_START → ADDED', () => {
|
||||
const confirm = scanReducer(stateOf('confirm'), { type: 'PREVIEW', preview })
|
||||
const confirm = scanReducer(stateOf('confirm'), { type: 'PREVIEW', preview, candidateId: 1001 })
|
||||
const starting = scanReducer(confirm, { type: 'ADD_START' })
|
||||
if (starting.phase === 'confirm') expect(starting.adding).toBe(true)
|
||||
const added = scanReducer(starting, { type: 'ADDED', item })
|
||||
@@ -122,7 +149,7 @@ describe('scanReducer', () => {
|
||||
})
|
||||
|
||||
it('ADD_ERROR stores the message without leaving confirm', () => {
|
||||
const confirm = scanReducer(stateOf('confirm'), { type: 'PREVIEW', preview })
|
||||
const confirm = scanReducer(stateOf('confirm'), { type: 'PREVIEW', preview, candidateId: 1001 })
|
||||
const starting = scanReducer(confirm, { type: 'ADD_START' })
|
||||
const next = scanReducer(starting, { type: 'ADD_ERROR', message: 'duplicate' })
|
||||
if (next.phase === 'confirm') {
|
||||
|
||||
Reference in New Issue
Block a user