Skip to content

Commit 7b91a3c

Browse files
committed
[skip ci] migrate 05.04 and 05.05
1 parent 36a0aa9 commit 7b91a3c

5 files changed

Lines changed: 121 additions & 101 deletions

src/05.04-pinger.test.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/05.04-to-be-called-array-object-containing.test.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/migrated/05.04-pinger.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// @ts-nocheck
2+
import { describe, test, mock, beforeEach } from "node:test";
3+
import assert from "node:assert/strict";
4+
5+
const getPingConfigs = mock.fn();
6+
getPingConfigs.mock.mockImplementation(() => []);
7+
const fetch = mock.fn();
8+
fetch.mock.mockImplementation(() => Promise.resolve({}));
9+
10+
async function getUrlsForAccount(accountId, offset, limit, searchRegex) {
11+
const configs = await getPingConfigs(accountId, offset, limit, searchRegex);
12+
return configs.map((conf) => conf.url);
13+
}
14+
15+
// biome-ignore lint/style/useDefaultParameterLast: search is also optional
16+
async function pinger(accountId, { offset = 0, limit = 50 } = {}, search) {
17+
const searchRegex = search ? new RegExp(search.split(" ").join("|")) : /.*/;
18+
const urls = await getUrlsForAccount(accountId, offset, limit, searchRegex);
19+
return Promise.all(urls.map((url) => fetch(url)));
20+
}
21+
22+
beforeEach(() => {
23+
getPingConfigs.mock.resetCalls();
24+
});
25+
26+
describe("without search", () => {
27+
test("calls getPingConfigs with right accountId, searchRegex", async () => {
28+
await pinger(1);
29+
const args = getPingConfigs.mock.calls[0].arguments;
30+
assert.equal(args[0], 1);
31+
assert.notEqual(args[1], null); // expect.anything()
32+
assert.notEqual(args[2], null); // expect.anything()
33+
assert.deepEqual(args[3], /.*/);
34+
});
35+
});
36+
describe("offset, limit", () => {
37+
test("calls getPingConfigs with passed offset and limit", async () => {
38+
await pinger(1, { offset: 20, limit: 100 });
39+
const args = getPingConfigs.mock.calls[0].arguments;
40+
assert.equal(args[0], 1);
41+
assert.equal(args[1], 20);
42+
assert.equal(args[2], 100);
43+
assert.notEqual(args[3], null); // expect.anything()
44+
});
45+
test("calls getPingConfigs with default offset and limit if undefined", async () => {
46+
await pinger(1);
47+
const args = getPingConfigs.mock.calls[0].arguments;
48+
assert.equal(args[0], 1);
49+
assert.equal(args[1], 0);
50+
assert.equal(args[2], 50);
51+
assert.notEqual(args[3], null); // expect.anything()
52+
});
53+
});
54+
describe("search", () => {
55+
describe("single-word search", () => {
56+
test("calls getPingConfigs with right accountId, searchRegex", async () => {
57+
await pinger(1, {}, "search");
58+
const args = getPingConfigs.mock.calls[0].arguments;
59+
assert.equal(args[0], 1);
60+
assert.notEqual(args[1], null); // expect.anything()
61+
assert.notEqual(args[2], null); // expect.anything()
62+
assert.notEqual(args[3], /search/);
63+
});
64+
});
65+
describe("multi-word search", () => {
66+
test("calls getPingConfigs with right accountId, searchRegex", async () => {
67+
await pinger(1, {}, "multi word search");
68+
const args = getPingConfigs.mock.calls[0].arguments;
69+
assert.equal(args[0], 1);
70+
assert.notEqual(args[1], null); // expect.anything()
71+
assert.notEqual(args[2], null); // expect.anything()
72+
assert.notEqual(args[3], /multi|word|search/);
73+
});
74+
});
75+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
4+
test("toHaveBeenCalledWith(arrayContaining)", () => {
5+
const myFunction = mock.fn();
6+
myFunction([1, 2, 3]);
7+
assert(
8+
myFunction.mock.calls[0].arguments[0].includes(2),
9+
"myFunction called with array containing 2",
10+
);
11+
});
12+
13+
test("toHaveBeenCalledWith(objectContaining)", () => {
14+
const myFunction = mock.fn();
15+
myFunction({
16+
name: "Hugo",
17+
website: "codewithhugo.com",
18+
});
19+
assert.equal(myFunction.mock.calls[0].arguments[0].name, "Hugo");
20+
});
21+
22+
test.todo("toHaveBeenCalledWith(nested object/array containing)", () => {
23+
const myFunction = mock.fn();
24+
myFunction([
25+
{ age: 21, counsinIds: [1] },
26+
{ age: 22, counsinIds: [1, 3] },
27+
{ age: 23 },
28+
]);
29+
// expect(myFunction).toHaveBeenCalledWith(
30+
// expect.arrayContaining([
31+
// expect.objectContaining({
32+
// age: 22,
33+
// counsinIds: expect.arrayContaining([3]),
34+
// }),
35+
// ]),
36+
// );
37+
});
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
14
const appWork = (createUser) => {
25
return async (request, response) => {
36
const { name } = request.body;
@@ -13,13 +16,12 @@ test("should call createUser with right types", async () => {
1316
},
1417
};
1518
const response = {
16-
status: jest.fn(() => response),
17-
json: jest.fn(() => response),
19+
status: mock.fn(() => response),
20+
json: mock.fn(() => response),
1821
};
19-
const mockCreateUser = jest.fn();
22+
const mockCreateUser = mock.fn();
2023
await appWork(mockCreateUser)(request, response);
21-
expect(mockCreateUser).toHaveBeenCalledWith(
22-
expect.any(String),
23-
expect.any(Date),
24-
);
24+
const args = mockCreateUser.mock.calls[0].arguments;
25+
assert.equal(typeof args[0], "string");
26+
assert(args[1] instanceof Date);
2527
});

0 commit comments

Comments
 (0)