|
1 | 1 | import { mapCreatorListItem } from './creator-list-item.mapper'; |
2 | | -import { requestContextStorage } from '../../utils/als.utils'; |
3 | | -import { logger } from '../../utils/logger.utils'; |
4 | | - |
5 | | -jest.mock('../../utils/logger.utils', () => ({ |
6 | | - logger: { warn: jest.fn(), error: jest.fn() }, |
7 | | -})); |
8 | | - |
9 | | -const warnMock = logger.warn as jest.Mock; |
10 | | -const errorMock = logger.error as jest.Mock; |
11 | | - |
12 | | -beforeEach(() => { |
13 | | - warnMock.mockClear(); |
14 | | - errorMock.mockClear(); |
15 | | -}); |
| 2 | +import { createSeededCreatorFixture } from '../../utils/test/seeded-creator-fixtures.utils'; |
16 | 3 |
|
17 | 4 | describe('mapCreatorListItem()', () => { |
18 | 5 | it('maps the public creator list item shape', () => { |
19 | | - const input = { |
20 | | - id: '1', |
21 | | - displayName: 'John', |
22 | | - avatarUrl: null, |
23 | | - createdAt: new Date('2024-01-02T03:04:05.678Z'), |
24 | | - updatedAt: new Date('2024-01-03T03:04:05.678Z'), |
25 | | - } as any; |
| 6 | + const input = createSeededCreatorFixture(1); |
26 | 7 |
|
27 | 8 | const result = mapCreatorListItem(input); |
28 | 9 |
|
29 | 10 | expect(result).toEqual({ |
30 | | - id: '1', |
31 | | - name: 'John', |
32 | | - avatar: null, |
33 | | - followers: 0, |
34 | | - createdAt: '2024-01-02T03:04:05.678Z', |
35 | | - updatedAt: '2024-01-03T03:04:05.678Z', |
36 | | - }); |
37 | | - expect(warnMock).not.toHaveBeenCalled(); |
38 | | - }); |
39 | | - |
40 | | - it('warns when a schema-required creator field is unexpectedly null', () => { |
41 | | - const input = { |
42 | 11 | id: 'creator-1', |
43 | | - displayName: null, |
44 | | - avatarUrl: null, |
45 | | - createdAt: new Date('2024-01-02T03:04:05.678Z'), |
46 | | - updatedAt: new Date('2024-01-03T03:04:05.678Z'), |
47 | | - } as any; |
48 | | - |
49 | | - const result = requestContextStorage.run( |
50 | | - { path: '/api/v1/creators', method: 'GET', requestId: 'req-333' }, |
51 | | - () => mapCreatorListItem(input) |
52 | | - ); |
53 | | - |
54 | | - expect(result).toEqual({ |
55 | | - id: 'creator-1', |
56 | | - name: null, |
57 | | - avatar: null, |
| 12 | + name: 'Creator 1', |
| 13 | + avatar: 'https://example.com/avatar-1.png', |
58 | 14 | followers: 0, |
59 | | - createdAt: '2024-01-02T03:04:05.678Z', |
60 | | - updatedAt: '2024-01-03T03:04:05.678Z', |
61 | 15 | }); |
62 | | - expect(warnMock).toHaveBeenCalledWith({ |
63 | | - msg: 'Unexpected null creator field in database result', |
64 | | - fieldName: 'displayName', |
65 | | - creatorId: 'creator-1', |
66 | | - requestId: 'req-333', |
67 | | - }); |
68 | | - }); |
69 | | - |
70 | | - it('logs an error when a string field receives an unexpected type', () => { |
71 | | - const input = { |
72 | | - id: 'creator-2', |
73 | | - handle: 'test-handle', |
74 | | - displayName: 12345, // number instead of string |
75 | | - avatarUrl: null, |
76 | | - isVerified: false, |
77 | | - createdAt: new Date('2024-01-02T03:04:05.678Z'), |
78 | | - updatedAt: new Date('2024-01-03T03:04:05.678Z'), |
79 | | - } as any; |
80 | | - |
81 | | - const result = requestContextStorage.run( |
82 | | - { path: '/api/v1/creators', method: 'GET', requestId: 'req-type-1' }, |
83 | | - () => mapCreatorListItem(input) |
84 | | - ); |
85 | | - |
86 | | - expect(result).toEqual({ |
87 | | - id: 'creator-2', |
88 | | - name: 12345, |
89 | | - avatar: null, |
90 | | - followers: 0, |
91 | | - createdAt: '2024-01-02T03:04:05.678Z', |
92 | | - updatedAt: '2024-01-03T03:04:05.678Z', |
93 | | - }); |
94 | | - expect(errorMock).toHaveBeenCalledWith({ |
95 | | - msg: 'Creator list field type mismatch', |
96 | | - fieldName: 'displayName', |
97 | | - expectedType: 'string', |
98 | | - receivedType: 'number', |
99 | | - creatorId: 'creator-2', |
100 | | - requestId: 'req-type-1', |
101 | | - }); |
102 | | - }); |
103 | | - |
104 | | - it('logs an error when a Date field receives a string', () => { |
105 | | - const input = { |
106 | | - id: 'creator-3', |
107 | | - handle: 'test-handle', |
108 | | - displayName: 'Alice', |
109 | | - avatarUrl: null, |
110 | | - isVerified: true, |
111 | | - createdAt: '2024-01-02T03:04:05.678Z', // string instead of Date |
112 | | - updatedAt: new Date('2024-01-03T03:04:05.678Z'), |
113 | | - } as any; |
114 | | - |
115 | | - requestContextStorage.run( |
116 | | - { path: '/api/v1/creators', method: 'GET', requestId: 'req-type-2' }, |
117 | | - () => mapCreatorListItem(input) |
118 | | - ); |
119 | | - |
120 | | - expect(errorMock).toHaveBeenCalledWith( |
121 | | - expect.objectContaining({ |
122 | | - msg: 'Creator list field type mismatch', |
123 | | - fieldName: 'createdAt', |
124 | | - expectedType: 'Date', |
125 | | - receivedType: 'string', |
126 | | - creatorId: 'creator-3', |
127 | | - requestId: 'req-type-2', |
128 | | - }) |
129 | | - ); |
130 | | - }); |
131 | | - |
132 | | - it('does not log an error for correctly typed fields', () => { |
133 | | - const input = { |
134 | | - id: 'creator-4', |
135 | | - handle: 'good-handle', |
136 | | - displayName: 'Bob', |
137 | | - avatarUrl: 'https://example.com/avatar.png', |
138 | | - isVerified: false, |
139 | | - createdAt: new Date('2024-01-02T03:04:05.678Z'), |
140 | | - updatedAt: new Date('2024-01-03T03:04:05.678Z'), |
141 | | - } as any; |
142 | | - |
143 | | - mapCreatorListItem(input); |
144 | | - |
145 | | - expect(errorMock).not.toHaveBeenCalled(); |
146 | 16 | }); |
147 | 17 | }); |
0 commit comments