Skip to content

Commit 3eb4a97

Browse files
author
Evgeniya Bashieva
committed
feat: add demo for lesson 12
1 parent 0243d9c commit 3eb4a97

File tree

11 files changed

+4353
-0
lines changed

11 files changed

+4353
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { createToken } from "../src/tokenService.js";
2+
3+
describe("tokenService", () => {
4+
describe("createToken", () => {
5+
test("should create token with correct format", () => {
6+
const userId = "test-user-123";
7+
const token = createToken(userId);
8+
9+
expect(token).toHaveProperty("token");
10+
expect(token).toHaveProperty("expiresIn");
11+
expect(token.token).toMatch(/^token-test-user-123-\d+$/);
12+
expect(token.expiresIn).toBe(3600);
13+
});
14+
15+
test("should create unique tokens for same user", () => {
16+
const userId = "user1";
17+
const token1 = createToken(userId);
18+
19+
// Небольшая задержка чтобы timestamp отличался
20+
setTimeout(() => {
21+
const token2 = createToken(userId);
22+
expect(token1.token).not.toBe(token2.token);
23+
}, 1);
24+
});
25+
26+
test("should handle different user IDs", () => {
27+
const token1 = createToken("user1");
28+
const token2 = createToken("user2");
29+
30+
expect(token1.token).toContain("user1");
31+
expect(token2.token).toContain("user2");
32+
expect(token1.token).not.toBe(token2.token);
33+
});
34+
35+
test("should return consistent expiresIn value", () => {
36+
const token1 = createToken("user1");
37+
const token2 = createToken("user2");
38+
39+
expect(token1.expiresIn).toBe(3600);
40+
expect(token2.expiresIn).toBe(3600);
41+
});
42+
});
43+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { sum, isAdult } from "../src/utils.js";
2+
3+
describe("utils", () => {
4+
describe("sum", () => {
5+
test("should add two numbers correctly", () => {
6+
expect(sum(2, 3)).toBe(5);
7+
expect(sum(-1, 1)).toBe(0);
8+
expect(sum(0, 0)).toBe(0);
9+
});
10+
11+
test("should handle negative numbers", () => {
12+
expect(sum(-5, -3)).toBe(-8);
13+
expect(sum(-10, 5)).toBe(-5);
14+
});
15+
16+
test("should handle decimal numbers", () => {
17+
expect(sum(1.5, 2.5)).toBe(4);
18+
expect(sum(0.1, 0.2)).toBeCloseTo(0.3);
19+
});
20+
});
21+
22+
describe("isAdult", () => {
23+
test("should return true for adults", () => {
24+
/** @type {User} */
25+
const adult = { id: "1", name: "John", age: 25, isActive: true };
26+
expect(isAdult(adult)).toBe(true);
27+
});
28+
29+
test("should return false for minors", () => {
30+
/** @type {User} */
31+
const minor = { id: "2", name: "Jane", age: 16, isActive: true };
32+
expect(isAdult(minor)).toBe(false);
33+
});
34+
35+
test("should return true for exactly 18 years old", () => {
36+
/** @type {User} */
37+
const eighteenYearOld = { id: "3", name: "Bob", age: 18, isActive: true };
38+
expect(isAdult(eighteenYearOld)).toBe(true);
39+
});
40+
41+
test("should handle edge cases", () => {
42+
/** @type {User} */
43+
const almostAdult = { id: "4", name: "Alice", age: 17, isActive: true };
44+
expect(isAdult(almostAdult)).toBe(false);
45+
46+
/** @type {User} */
47+
const veryOld = { id: "5", name: "Elder", age: 100, isActive: false };
48+
expect(isAdult(veryOld)).toBe(true);
49+
});
50+
});
51+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/** @type {import('jest').Config} */
2+
const config = {
3+
// Среда выполнения тестов (node для серверного кода)
4+
testEnvironment: "node",
5+
6+
// Поддержка ES модулей, так как в package.json указан "type": "module"
7+
preset: "ts-jest/presets/default-esm",
8+
9+
// Паттерны для поиска тестовых файлов
10+
testMatch: [
11+
"**/__tests__/**/*.js", // Файлы в папке __tests__
12+
"**/?(*.)+(spec|test).js", // Файлы с суффиксами .test.js или .spec.js
13+
],
14+
15+
// Настройки покрытия кода
16+
collectCoverage: true,
17+
coverageDirectory: "coverage",
18+
coverageReporters: ["text", "lcov", "html"],
19+
20+
// Исключить из анализа покрытия
21+
coveragePathIgnorePatterns: ["/node_modules/", "/coverage/", "/docs/"],
22+
23+
// Настройки для работы с ES модулями (новый синтаксис)
24+
transform: {
25+
"^.+\\.js$": [
26+
"ts-jest",
27+
{
28+
useESM: true,
29+
},
30+
],
31+
},
32+
33+
// Расширения файлов для разрешения модулей
34+
moduleFileExtensions: ["js", "json"],
35+
36+
// Verbose вывод для детальной информации о тестах
37+
verbose: true,
38+
39+
// Очистка моков между тестами
40+
clearMocks: true,
41+
42+
// Показать покрытие в консоли
43+
collectCoverageFrom: [
44+
"src/**/*.js",
45+
"!src/index.js", // Исключить главный файл из анализа покрытия
46+
],
47+
};
48+
49+
export default config;

0 commit comments

Comments
 (0)