|
1 | 1 | import { TableDataClient } from '@common'; |
2 | 2 | import * as TableDataFormDataBuilder from '../table-data-form-data-builder/table-data-form-data-builder'; |
| 3 | +import { isErrorResponse } from './table-data-client'; |
3 | 4 |
|
4 | 5 | describe('TableDataClient', () => { |
5 | | - let dataTableFormDataBuilder; |
6 | | - let url; |
7 | | - let formData; |
8 | | - let bugSplatApiClient; |
9 | | - let response; |
10 | | - |
11 | | - let service: TableDataClient; |
12 | | - |
13 | | - beforeEach(() => { |
14 | | - url = 'https://woot.com'; |
15 | | - formData = { form: 'data!' }; |
16 | | - response = [{ Rows: [{ yee: 'ha!' }], PageData: { woo: 'hoo!' } }]; |
17 | | - dataTableFormDataBuilder = jasmine.createSpyObj('DataTableFormDataBuilder', [ |
18 | | - 'withDatabase', |
19 | | - 'withColumnGroups', |
20 | | - 'withFilterGroups', |
21 | | - 'withPage', |
22 | | - 'withPageSize', |
23 | | - 'withSortColumn', |
24 | | - 'withSortOrder', |
25 | | - 'build' |
26 | | - ]); |
27 | | - dataTableFormDataBuilder.withDatabase.and.returnValue(dataTableFormDataBuilder); |
28 | | - dataTableFormDataBuilder.withColumnGroups.and.returnValue(dataTableFormDataBuilder); |
29 | | - dataTableFormDataBuilder.withFilterGroups.and.returnValue(dataTableFormDataBuilder); |
30 | | - dataTableFormDataBuilder.withPage.and.returnValue(dataTableFormDataBuilder); |
31 | | - dataTableFormDataBuilder.withPageSize.and.returnValue(dataTableFormDataBuilder); |
32 | | - dataTableFormDataBuilder.withSortColumn.and.returnValue(dataTableFormDataBuilder); |
33 | | - dataTableFormDataBuilder.withSortOrder.and.returnValue(dataTableFormDataBuilder); |
34 | | - dataTableFormDataBuilder.build.and.returnValue(formData); |
35 | | - spyOn(TableDataFormDataBuilder, 'TableDataFormDataBuilder').and.returnValue(dataTableFormDataBuilder); |
36 | | - |
37 | | - const json = () => response; |
38 | | - bugSplatApiClient = jasmine.createSpyObj('BugSplatApiClient', ['fetch']); |
39 | | - bugSplatApiClient.fetch.and.resolveTo({ json }); |
40 | | - |
41 | | - service = new TableDataClient(bugSplatApiClient, url); |
42 | | - }); |
43 | | - |
44 | | - it('should be created', () => { |
45 | | - expect(service).toBeTruthy(); |
46 | | - }); |
47 | | - |
48 | | - describe('getData', () => { |
49 | | - let database; |
50 | | - let columnGroups; |
51 | | - let filterGroups; |
52 | | - let page; |
53 | | - let pageSize; |
54 | | - let sortColumn; |
55 | | - let sortOrder; |
56 | | - let result; |
57 | | - |
58 | | - beforeEach(async () => { |
59 | | - database = 'Black Rifle Coffee'; |
60 | | - columnGroups = ['group', 'group again']; |
61 | | - filterGroups = ['filter', 'anotha 1']; |
62 | | - page = 9; |
63 | | - pageSize = 9001; |
64 | | - sortColumn = 'sorty'; |
65 | | - sortOrder = 'mcOrder'; |
66 | | - const response = await service.postGetData({ |
67 | | - database, |
68 | | - columnGroups, |
69 | | - filterGroups, |
70 | | - page, |
71 | | - pageSize, |
72 | | - sortColumn, |
73 | | - sortOrder |
74 | | - }); |
75 | | - result = await response.json(); |
| 6 | + let dataTableFormDataBuilder; |
| 7 | + let url; |
| 8 | + let formData; |
| 9 | + let bugSplatApiClient; |
| 10 | + let response; |
| 11 | + |
| 12 | + let service: TableDataClient; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + url = 'https://woot.com'; |
| 16 | + formData = { form: 'data!' }; |
| 17 | + response = { rows: [{ yee: 'ha!' }], pageData: { woo: 'hoo!' } }; |
| 18 | + dataTableFormDataBuilder = jasmine.createSpyObj('DataTableFormDataBuilder', [ |
| 19 | + 'withDatabase', |
| 20 | + 'withColumnGroups', |
| 21 | + 'withFilterGroups', |
| 22 | + 'withPage', |
| 23 | + 'withPageSize', |
| 24 | + 'withSortColumn', |
| 25 | + 'withSortOrder', |
| 26 | + 'build', |
| 27 | + ]); |
| 28 | + dataTableFormDataBuilder.withDatabase.and.returnValue(dataTableFormDataBuilder); |
| 29 | + dataTableFormDataBuilder.withColumnGroups.and.returnValue(dataTableFormDataBuilder); |
| 30 | + dataTableFormDataBuilder.withFilterGroups.and.returnValue(dataTableFormDataBuilder); |
| 31 | + dataTableFormDataBuilder.withPage.and.returnValue(dataTableFormDataBuilder); |
| 32 | + dataTableFormDataBuilder.withPageSize.and.returnValue(dataTableFormDataBuilder); |
| 33 | + dataTableFormDataBuilder.withSortColumn.and.returnValue(dataTableFormDataBuilder); |
| 34 | + dataTableFormDataBuilder.withSortOrder.and.returnValue(dataTableFormDataBuilder); |
| 35 | + dataTableFormDataBuilder.build.and.returnValue(formData); |
| 36 | + spyOn(TableDataFormDataBuilder, 'TableDataFormDataBuilder').and.returnValue( |
| 37 | + dataTableFormDataBuilder |
| 38 | + ); |
| 39 | + |
| 40 | + const json = () => response; |
| 41 | + bugSplatApiClient = jasmine.createSpyObj('BugSplatApiClient', ['fetch']); |
| 42 | + bugSplatApiClient.fetch.and.resolveTo({ status: 200, json }); |
| 43 | + |
| 44 | + service = new TableDataClient(bugSplatApiClient, url); |
76 | 45 | }); |
77 | 46 |
|
78 | | - it('should call DataTaableFormDataBuilder with database', () => { |
79 | | - expect(dataTableFormDataBuilder.withDatabase).toHaveBeenCalledWith(database); |
| 47 | + it('should be created', () => { |
| 48 | + expect(service).toBeTruthy(); |
80 | 49 | }); |
81 | 50 |
|
82 | | - it('should call DatatableFormDataBuilder with columng groups', () => { |
83 | | - expect(dataTableFormDataBuilder.withColumnGroups).toHaveBeenCalledWith(columnGroups); |
| 51 | + describe('getData', () => { |
| 52 | + let database; |
| 53 | + let columnGroups; |
| 54 | + let filterGroups; |
| 55 | + let page; |
| 56 | + let pageSize; |
| 57 | + let sortColumn; |
| 58 | + let sortOrder; |
| 59 | + let result; |
| 60 | + |
| 61 | + beforeEach(async () => { |
| 62 | + database = 'Black Rifle Coffee'; |
| 63 | + columnGroups = ['group', 'group again']; |
| 64 | + filterGroups = ['filter', 'anotha 1']; |
| 65 | + page = 9; |
| 66 | + pageSize = 9001; |
| 67 | + sortColumn = 'sorty'; |
| 68 | + sortOrder = 'mcOrder'; |
| 69 | + const response = await service.postGetData({ |
| 70 | + database, |
| 71 | + columnGroups, |
| 72 | + filterGroups, |
| 73 | + page, |
| 74 | + pageSize, |
| 75 | + sortColumn, |
| 76 | + sortOrder, |
| 77 | + }); |
| 78 | + if (isErrorResponse(response)) { |
| 79 | + throw new Error((await response.json()).message); |
| 80 | + } |
| 81 | + result = await response.json(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should call DataTaableFormDataBuilder with database', () => { |
| 85 | + expect(dataTableFormDataBuilder.withDatabase).toHaveBeenCalledWith(database); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should call DatatableFormDataBuilder with columng groups', () => { |
| 89 | + expect(dataTableFormDataBuilder.withColumnGroups).toHaveBeenCalledWith(columnGroups); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should call DataTableFormDataBuilder with filter groups', () => { |
| 93 | + expect(dataTableFormDataBuilder.withFilterGroups).toHaveBeenCalledWith(filterGroups); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should call DataTableFormDataBuilder with page', () => { |
| 97 | + expect(dataTableFormDataBuilder.withPage).toHaveBeenCalledWith(page); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should call DataTableFormDataBuilder with page size', () => { |
| 101 | + expect(dataTableFormDataBuilder.withPageSize).toHaveBeenCalledWith(pageSize); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should call DataTableFormDataBuilder with sort column', () => { |
| 105 | + expect(dataTableFormDataBuilder.withSortColumn).toHaveBeenCalledWith(sortColumn); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should call DataTableFormDataBuilder with sort order', () => { |
| 109 | + expect(dataTableFormDataBuilder.withSortOrder).toHaveBeenCalledWith(sortOrder); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should call build on DataTableFormDataBuilder', () => { |
| 113 | + expect(dataTableFormDataBuilder.build).toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should call fetch with correct url', () => { |
| 117 | + expect(bugSplatApiClient.fetch).toHaveBeenCalledWith(url, jasmine.anything()); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should call fetch with form data from DataTableFormDataBuilder', () => { |
| 121 | + expect(bugSplatApiClient.fetch).toHaveBeenCalledWith( |
| 122 | + jasmine.anything(), |
| 123 | + jasmine.objectContaining({ |
| 124 | + body: formData, |
| 125 | + }) |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should return value from api', () => { |
| 130 | + expect(result).toEqual( |
| 131 | + jasmine.objectContaining({ |
| 132 | + rows: response.rows, |
| 133 | + pageData: response.pageData, |
| 134 | + }) |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should throw if api returns error response', async () => { |
| 139 | + const expectedErrorMessage = 'Internal Server Error'; |
| 140 | + bugSplatApiClient.fetch.and.resolveTo({ |
| 141 | + status: 500, |
| 142 | + message: expectedErrorMessage, |
| 143 | + }); |
| 144 | + try { |
| 145 | + await service.postGetData({ |
| 146 | + filterGroups, |
| 147 | + page, |
| 148 | + pageSize, |
| 149 | + sortColumn, |
| 150 | + sortOrder, |
| 151 | + }); |
| 152 | + } catch (error: any) { |
| 153 | + expect(error).toBeInstanceOf(Error); |
| 154 | + expect(error.message).toBe(expectedErrorMessage); |
| 155 | + } |
| 156 | + }); |
84 | 157 | }); |
85 | | - |
86 | | - it('should call DataTableFormDataBuilder with filter groups', () => { |
87 | | - expect(dataTableFormDataBuilder.withFilterGroups).toHaveBeenCalledWith(filterGroups); |
88 | | - }); |
89 | | - |
90 | | - it('should call DataTableFormDataBuilder with page', () => { |
91 | | - expect(dataTableFormDataBuilder.withPage).toHaveBeenCalledWith(page); |
92 | | - }); |
93 | | - |
94 | | - it('should call DataTableFormDataBuilder with page size', () => { |
95 | | - expect(dataTableFormDataBuilder.withPageSize).toHaveBeenCalledWith(pageSize); |
96 | | - }); |
97 | | - |
98 | | - it('should call DataTableFormDataBuilder with sort column', () => { |
99 | | - expect(dataTableFormDataBuilder.withSortColumn).toHaveBeenCalledWith(sortColumn); |
100 | | - }); |
101 | | - |
102 | | - it('should call DataTableFormDataBuilder with sort order', () => { |
103 | | - expect(dataTableFormDataBuilder.withSortOrder).toHaveBeenCalledWith(sortOrder); |
104 | | - }); |
105 | | - |
106 | | - it('should call build on DataTableFormDataBuilder', () => { |
107 | | - expect(dataTableFormDataBuilder.build).toHaveBeenCalled(); |
108 | | - }); |
109 | | - |
110 | | - it('should call fetch with correct url', () => { |
111 | | - expect(bugSplatApiClient.fetch).toHaveBeenCalledWith(url, jasmine.anything()); |
112 | | - }); |
113 | | - |
114 | | - it('should call fetch with form data from DataTableFormDataBuilder', () => { |
115 | | - expect(bugSplatApiClient.fetch).toHaveBeenCalledWith( |
116 | | - jasmine.anything(), |
117 | | - jasmine.objectContaining({ |
118 | | - body: formData |
119 | | - }) |
120 | | - ); |
121 | | - }); |
122 | | - |
123 | | - it('should return value from api', () => { |
124 | | - expect(result).toEqual(jasmine.objectContaining({ |
125 | | - rows: response[0].Rows, |
126 | | - pageData: response[0].PageData |
127 | | - })); |
128 | | - }); |
129 | | - |
130 | | - it('should return empty array if api returns null', async () => { |
131 | | - bugSplatApiClient.fetch.and.resolveTo({ json: () => null }); |
132 | | - |
133 | | - const response = await service.postGetData({ |
134 | | - filterGroups, |
135 | | - page, |
136 | | - pageSize, |
137 | | - sortColumn, |
138 | | - sortOrder |
139 | | - }); |
140 | | - result = await response.json(); |
141 | | - |
142 | | - expect(result).toEqual(jasmine.objectContaining({ |
143 | | - rows: [], |
144 | | - pageData: {} |
145 | | - })); |
146 | | - }); |
147 | | - }); |
148 | 158 | }); |
0 commit comments