-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support another error and add tests
- Loading branch information
1 parent
d718873
commit 3c6bf36
Showing
4 changed files
with
127 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import {describe, it, expect, beforeEach} from 'vitest'; | ||
import {ApiException} from '@myparcel/sdk'; | ||
import {type TranslatableWithArgs} from '../types'; | ||
import {ERROR_INVALID_POSTAL_CODE, IGNORED_ERRORS, ERROR_MISSING_REQUIRED_PARAMETER, ERROR_REPLACE_MAP} from '../data'; | ||
import {useApiExceptions} from './useApiExceptions'; | ||
|
||
const createException = (code: string | number, message = 'error'): ApiException => { | ||
return new ApiException({ | ||
message: 'error', | ||
request_id: '...', | ||
errors: [{code: Number(code), message}], | ||
}); | ||
}; | ||
|
||
describe('useApiExceptions', () => { | ||
beforeEach(() => { | ||
useApiExceptions().clear(); | ||
}); | ||
|
||
it('adds exceptions', () => { | ||
const {addException, exceptions, hasExceptions} = useApiExceptions(); | ||
const exception = createException(ERROR_INVALID_POSTAL_CODE); | ||
|
||
addException(['test'], exception); | ||
|
||
expect(hasExceptions.value).toBe(true); | ||
expect(exceptions.value).toEqual([ | ||
{ | ||
code: ERROR_INVALID_POSTAL_CODE, | ||
label: `error${ERROR_INVALID_POSTAL_CODE}`, | ||
}, | ||
]); | ||
|
||
addException(['test'], createException(ERROR_INVALID_POSTAL_CODE)); | ||
|
||
// Expect no duplicates | ||
expect(exceptions.value).toHaveLength(1); | ||
}); | ||
|
||
it.each(IGNORED_ERRORS)('ignores exceptions with code %s', (code) => { | ||
const {addException, exceptions, hasExceptions} = useApiExceptions(); | ||
const exception = createException(code); | ||
|
||
addException(['someRequest'], exception); | ||
|
||
expect(hasExceptions.value).toBe(false); | ||
expect(exceptions.value).toEqual([]); | ||
}); | ||
|
||
it.each(Object.entries(ERROR_REPLACE_MAP))(`replaces error code %d with %d`, (code, replacement) => { | ||
const {addException, exceptions, hasExceptions} = useApiExceptions(); | ||
const exception = createException(code); | ||
|
||
addException(['test'], exception); | ||
|
||
expect(hasExceptions.value).toBe(true); | ||
expect(exceptions.value).toEqual([ | ||
{ | ||
code: replacement, | ||
label: `error${replacement}`, | ||
}, | ||
]); | ||
}); | ||
|
||
it('can clear exceptions', () => { | ||
const {addException, clear, hasExceptions, exceptions} = useApiExceptions(); | ||
const exception = createException(ERROR_INVALID_POSTAL_CODE); | ||
|
||
addException(['test'], exception); | ||
|
||
expect(hasExceptions.value).toBe(true); | ||
|
||
clear(); | ||
|
||
expect(hasExceptions.value).toBe(false); | ||
expect(exceptions.value).toEqual([]); | ||
}); | ||
|
||
it(`adds arguments to exception with code ${ERROR_MISSING_REQUIRED_PARAMETER}`, () => { | ||
const {addException, exceptions, hasExceptions} = useApiExceptions(); | ||
const exception = createException(ERROR_MISSING_REQUIRED_PARAMETER, 'city is required'); | ||
|
||
addException(['test'], exception); | ||
|
||
expect(hasExceptions.value).toBe(true); | ||
expect(exceptions.value).toEqual([ | ||
{ | ||
code: ERROR_MISSING_REQUIRED_PARAMETER, | ||
label: { | ||
key: `error${ERROR_MISSING_REQUIRED_PARAMETER}`, | ||
args: { | ||
field: { | ||
key: 'city', | ||
plain: true, | ||
}, | ||
}, | ||
} satisfies TranslatableWithArgs, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters