Skip to content

Commit 8702202

Browse files
committed
added test to verify search by baselineBranchName
1 parent bdc04d1 commit 8702202

File tree

1 file changed

+90
-135
lines changed

1 file changed

+90
-135
lines changed

src/test-variations/test-variations.service.spec.ts

Lines changed: 90 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -111,167 +111,98 @@ describe('TestVariationsService', () => {
111111
});
112112

113113
describe('find', () => {
114-
it('can find by main branch', async () => {
115-
const createRequest: CreateTestRequestDto = {
116-
buildId: 'buildId',
117-
projectId: projectMock.id,
118-
name: 'Test name',
119-
os: 'OS',
120-
browser: 'browser',
121-
viewport: 'viewport',
122-
device: 'device',
123-
customTags: '',
124-
branchName: 'develop',
125-
};
114+
const createRequest: CreateTestRequestDto = {
115+
buildId: 'buildId',
116+
projectId: projectMock.id,
117+
name: 'Test name',
118+
os: 'OS',
119+
browser: 'browser',
120+
viewport: 'viewport',
121+
device: 'device',
122+
customTags: '',
123+
branchName: 'develop',
124+
};
126125

127-
const variationMock: TestVariation = {
128-
id: '123',
129-
projectId: projectMock.id,
130-
name: 'Test name',
131-
baselineName: 'baselineName',
132-
os: 'OS',
133-
browser: 'browser',
134-
viewport: 'viewport',
135-
device: 'device',
136-
customTags: '',
137-
ignoreAreas: '[]',
138-
comment: 'some comment',
139-
branchName: 'develop',
140-
createdAt: new Date(),
141-
updatedAt: new Date(),
142-
};
126+
const variationMock: TestVariation = {
127+
id: '123',
128+
projectId: projectMock.id,
129+
name: 'Test name',
130+
baselineName: 'baselineName',
131+
os: 'OS',
132+
browser: 'browser',
133+
viewport: 'viewport',
134+
device: 'device',
135+
customTags: '',
136+
ignoreAreas: '[]',
137+
comment: 'some comment',
138+
branchName: 'develop',
139+
createdAt: new Date(),
140+
updatedAt: new Date(),
141+
};
142+
143+
const findRequestTemplate = {
144+
name: createRequest.name,
145+
projectId: createRequest.projectId,
146+
os: createRequest.os,
147+
browser: createRequest.browser,
148+
viewport: createRequest.viewport,
149+
device: createRequest.device,
150+
customTags: createRequest.customTags,
151+
};
152+
153+
it('can find by main branch', async () => {
154+
// .Arrange
143155
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
144156
service = await initModule({ projectFindUniqueMock });
145157
service.findUnique = jest.fn().mockResolvedValueOnce(variationMock).mockResolvedValueOnce(undefined);
146158

159+
// .Act
147160
const result = await service.find(createRequest);
148161

162+
// .Assert - use variation from main branch if no variation for feature branch
149163
expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
150164
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
151-
name: createRequest.name,
152-
projectId: createRequest.projectId,
153-
os: createRequest.os,
154-
browser: createRequest.browser,
155-
viewport: createRequest.viewport,
156-
device: createRequest.device,
157-
customTags: createRequest.customTags,
165+
...findRequestTemplate,
158166
branchName: projectMock.mainBranchName,
159167
});
160168
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
161-
name: createRequest.name,
162-
projectId: createRequest.projectId,
163-
os: createRequest.os,
164-
browser: createRequest.browser,
165-
viewport: createRequest.viewport,
166-
device: createRequest.device,
167-
customTags: createRequest.customTags,
169+
...findRequestTemplate,
168170
branchName: createRequest.branchName,
169171
});
170172
expect(result).toBe(variationMock);
171173
});
172174

173175
it('can find by current branch', async () => {
174-
const createRequest: CreateTestRequestDto = {
175-
buildId: 'buildId',
176-
projectId: projectMock.id,
177-
name: 'Test name',
178-
os: 'OS',
179-
browser: 'browser',
180-
viewport: 'viewport',
181-
device: 'device',
182-
customTags: '',
183-
branchName: 'develop',
184-
};
185-
186-
const variationMock: TestVariation = {
187-
id: '123',
188-
projectId: projectMock.id,
189-
name: 'Test name',
190-
baselineName: 'baselineName',
191-
os: 'OS',
192-
browser: 'browser',
193-
viewport: 'viewport',
194-
device: 'device',
195-
customTags: '',
196-
ignoreAreas: '[]',
197-
comment: 'some comment',
198-
branchName: 'develop',
199-
createdAt: new Date(),
200-
updatedAt: new Date(),
201-
};
176+
// .Arrange
202177
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
203178
service = await initModule({ projectFindUniqueMock });
204179
service.findUnique = jest.fn().mockResolvedValueOnce(undefined).mockResolvedValueOnce(variationMock);
205180

181+
// .Act
206182
const result = await service.find(createRequest);
207183

184+
// .Assert - use variation from feature branch if exists
208185
expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
209186
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
210-
name: createRequest.name,
211-
projectId: createRequest.projectId,
212-
os: createRequest.os,
213-
browser: createRequest.browser,
214-
viewport: createRequest.viewport,
215-
device: createRequest.device,
216-
customTags: createRequest.customTags,
187+
...findRequestTemplate,
217188
branchName: projectMock.mainBranchName,
218189
});
219190
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
220-
name: createRequest.name,
221-
projectId: createRequest.projectId,
222-
os: createRequest.os,
223-
browser: createRequest.browser,
224-
viewport: createRequest.viewport,
225-
device: createRequest.device,
226-
customTags: createRequest.customTags,
191+
...findRequestTemplate,
227192
branchName: createRequest.branchName,
228193
});
229194
expect(result).toBe(variationMock);
230195
});
231196

232197
it('can find by current branch but main branch is more relevant', async () => {
233-
const createRequest: CreateTestRequestDto = {
234-
buildId: 'buildId',
235-
projectId: projectMock.id,
236-
name: 'Test name',
237-
os: 'OS',
238-
browser: 'browser',
239-
viewport: 'viewport',
240-
device: 'device',
241-
customTags: '',
242-
branchName: 'develop',
243-
};
244-
198+
// .Arrange
245199
const variationMainMock: TestVariation = {
246-
id: '123',
247-
projectId: projectMock.id,
248-
name: 'Test name',
249-
baselineName: 'baselineName',
250-
os: 'OS',
251-
browser: 'browser',
252-
viewport: 'viewport',
253-
device: 'device',
254-
customTags: '',
255-
ignoreAreas: '[]',
256-
comment: 'some comment',
200+
...variationMock,
257201
branchName: 'master',
258-
createdAt: new Date(),
259-
updatedAt: new Date(),
260202
};
261203
const variationFeatureMock: TestVariation = {
262-
id: '123',
263-
projectId: projectMock.id,
264-
name: 'Test name',
265-
baselineName: 'baselineName',
266-
os: 'OS',
267-
browser: 'browser',
268-
viewport: 'viewport',
269-
device: 'device',
270-
customTags: '',
271-
ignoreAreas: '[]',
272-
comment: 'some comment',
204+
...variationMock,
273205
branchName: 'develop',
274-
createdAt: new Date(),
275206
updatedAt: new Date(variationMainMock.updatedAt.getDate() - 1),
276207
};
277208
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
@@ -281,31 +212,55 @@ describe('TestVariationsService', () => {
281212
.mockResolvedValueOnce(variationMainMock)
282213
.mockResolvedValueOnce(variationFeatureMock);
283214

215+
// .Act
284216
const result = await service.find(createRequest);
285217

218+
// .Assert - use variation from main branch if it updated later
286219
expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
287220
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
288-
name: createRequest.name,
289-
projectId: createRequest.projectId,
290-
os: createRequest.os,
291-
browser: createRequest.browser,
292-
viewport: createRequest.viewport,
293-
device: createRequest.device,
294-
customTags: createRequest.customTags,
221+
...findRequestTemplate,
295222
branchName: projectMock.mainBranchName,
296223
});
297224
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
298-
name: createRequest.name,
299-
projectId: createRequest.projectId,
300-
os: createRequest.os,
301-
browser: createRequest.browser,
302-
viewport: createRequest.viewport,
303-
device: createRequest.device,
304-
customTags: createRequest.customTags,
225+
...findRequestTemplate,
305226
branchName: createRequest.branchName,
306227
});
307228
expect(result).toBe(variationMainMock);
308229
});
230+
231+
it('can find by specified baseline branch', async () => {
232+
// .Arrange
233+
const createRequestWithBaseline = {
234+
...createRequest,
235+
baselineBranchName: 'baseline-branch',
236+
};
237+
const variationBaselineMock: TestVariation = {
238+
...variationMock,
239+
branchName: 'baseline-branch',
240+
};
241+
const variationFeatureMock: TestVariation = {
242+
...variationMock,
243+
branchName: 'develop',
244+
};
245+
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
246+
service = await initModule({ projectFindUniqueMock });
247+
service.findUnique = jest.fn().mockResolvedValueOnce(variationBaselineMock).mockResolvedValueOnce(undefined);
248+
249+
// .Act
250+
const result = await service.find(createRequestWithBaseline);
251+
252+
// .Assert - use variation from main branch if it updated later
253+
expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
254+
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
255+
...findRequestTemplate,
256+
branchName: createRequestWithBaseline.baselineBranchName,
257+
});
258+
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
259+
...findRequestTemplate,
260+
branchName: createRequest.branchName,
261+
});
262+
expect(result).toBe(variationBaselineMock);
263+
});
309264
});
310265

311266
it('create', async () => {

0 commit comments

Comments
 (0)