1
|
|
|
import nError, { ERROR_STATUS_TEXT_MAP } from '../creator'; |
2
|
|
|
|
3
|
|
|
describe('ERROR_STATUS_TEXT_MAP', () => { |
4
|
|
|
it('accessible via key name as number', () => { |
5
|
|
|
const type = ERROR_STATUS_TEXT_MAP[404]; |
6
|
|
|
expect(type).toBe('NOT_FOUND'); |
7
|
|
|
}); |
8
|
|
|
|
9
|
|
|
it('accessible via key name as string', () => { |
10
|
|
|
const type = ERROR_STATUS_TEXT_MAP['404']; |
11
|
|
|
expect(type).toBe('NOT_FOUND'); |
12
|
|
|
}); |
13
|
|
|
}); |
14
|
|
|
|
15
|
|
|
describe('nError', () => { |
16
|
|
|
describe('is a constructor function creates nError instance', () => { |
17
|
|
|
it('with .stack to trace the callStack', () => { |
18
|
|
|
const e = nError({ |
19
|
|
|
message: 'some message', |
20
|
|
|
type: 'SOME_TYPE', |
21
|
|
|
}); |
22
|
|
|
expect(e instanceof nError).toBe(true); |
23
|
|
|
expect(e.stack.length).toBeGreaterThan(0); |
24
|
|
|
expect(e).toMatchSnapshot(); |
25
|
|
|
}); |
26
|
|
|
|
27
|
|
|
it('exposes all fileds including .stack in Object.assign', () => { |
28
|
|
|
const e = nError({ message: 'test', a: 'a', b: 'b' }); |
29
|
|
|
const ee = Object.assign(e, { c: 'c' }); |
30
|
|
|
expect(ee.stack.length).toBeGreaterThan(0); |
31
|
|
|
expect(ee.stack).toBe(e.stack); |
32
|
|
|
expect(ee instanceof nError).toBe(true); |
33
|
|
|
expect(ee).toMatchSnapshot(); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
it('with .extend() prototype method that creates new instance and maintains the stack', () => { |
37
|
|
|
const e = nError({ message: 'some message' }); |
38
|
|
|
const ee = e.extend({ next: 'TEST' }); |
39
|
|
|
expect(ee).not.toBe(e); |
40
|
|
|
expect(ee.stack.toString()).toContain('__tests__/creator.js:37'); |
41
|
|
|
expect(ee.stack.length).toBeGreaterThan(0); |
42
|
|
|
expect(ee).toMatchSnapshot(); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
it('with .remove() prototype method that creates new instance and maintains the stack', () => { |
46
|
|
|
const e = nError({ |
47
|
|
|
message: 'some message', |
48
|
|
|
type: 'SOME_TYPE', |
49
|
|
|
}); |
50
|
|
|
const ee = e.remove('type'); |
51
|
|
|
expect(ee).not.toBe(e); |
52
|
|
|
expect(ee.stack.toString()).toContain('__tests__/creator.js:46'); |
53
|
|
|
expect(ee).toMatchSnapshot(); |
54
|
|
|
}); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
describe('function has methods as nError creator', () => { |
58
|
|
|
it('of status named after error type in camelCase', () => { |
59
|
|
|
const e = nError.notFound({ message: 'some error message' }); |
60
|
|
|
expect(e.status).toBe(404); |
61
|
|
|
expect(e).toMatchSnapshot(); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
it('for all status defined in ERROR_STATUS_TEXT_MAP', () => { |
65
|
|
|
const methods = Object.keys(nError); |
66
|
|
|
expect(methods).toHaveLength(Object.keys(ERROR_STATUS_TEXT_MAP).length); |
67
|
|
|
}); |
68
|
|
|
}); |
69
|
|
|
}); |
70
|
|
|
|