Skip to content

Add tests #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Enterprise/test/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import 'fake-indexeddb/auto';
import { Database } from '../static/database.js';
import { Repository, Service } from '../static/core.js';
import { UserModel, UserRepository, UserService } from '../static/user.js';
import { Repository } from '../static/core.js';
import { UserModel } from '../static/user.js';

test('Enterprise: Database CRUD + queries', async () => {
const db = new Database('TestDB', 1, (db) => {
Expand Down
37 changes: 34 additions & 3 deletions Enterprise/test/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import 'fake-indexeddb/auto';
import { Database } from '../static/database.js';
import { Repository, Service } from '../static/core.js';
import { UserModel, UserRepository, UserService } from '../static/user.js';

test('Enterprise: UserModel validation', async () => {
Expand All @@ -16,7 +15,36 @@ test('Enterprise: UserModel validation', async () => {
assert.strictEqual(user.age, 42);
});

test('Enterprise: UserService, UserRepository', async () => {
test('Enterprise: UserRepository', async () => {
const db = new Database('UserRepositoryTestDB', 1, (db) => {
if (!db.objectStoreNames.contains('user')) {
db.createObjectStore('user', { keyPath: 'id', autoIncrement: true });
}
});
await db.connect();

const userRepo = new UserRepository(db, 'user');

const user1 = new UserModel('Lucius', 17);
await userRepo.insert(user1);
const user2 = new UserModel('Antoninus', 33);
await userRepo.insert(user2);
const user3 = new UserModel('Faustina', 18);
await userRepo.insert(user3);

const user = await userRepo.get(1);
assert.equal(user.name, 'Lucius');

user.age += 1;
await userRepo.update(user);
assert.equal(user.age, 18);

const users = await userRepo.getAll();
assert.equal(users.length, 3);
assert.equal(users[1].age, 33);
});

test('Enterprise: UserService', async () => {
const db = new Database('ServiceTestDB', 1, (db) => {
if (!db.objectStoreNames.contains('user')) {
db.createObjectStore('user', { keyPath: 'id', autoIncrement: true });
Expand All @@ -38,5 +66,8 @@ test('Enterprise: UserService, UserRepository', async () => {
const updatedUser = await userService.incrementAge(2);
assert.equal(updatedUser.age, 34);

await assert.rejects(() => userService.incrementAge(999), /User with id=1 not found/);
await assert.rejects(
() => userService.incrementAge(999),
/User with id=1 not found/,
);
});