Skip to content
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
28 changes: 23 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"@rspack/core": "^2.0.3",
"@std/expect": "npm:@jsr/std__expect@^1.0.17",
"@testing-library/react": "^16.1.0",
"@trivago/prettier-plugin-sort-imports": "^6.0.2",
"@types/node": "^24.2.1",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
Expand Down
4 changes: 3 additions & 1 deletion scripts/setup-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ before(async context => {
return;
}

GlobalRegistrator.register();
GlobalRegistrator.register({
url: 'http://localhost:8080',
});

// dynamic import because https://github.com/capricorn86/happy-dom/issues/1636#issuecomment-2568308938
const { configure } = await import('@testing-library/react');
Expand Down
13 changes: 11 additions & 2 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ export function $(cmd: string): Promise<void> {
env: { ...process.env, FORCE_COLOR: '3' },
});

child.on('error', fail);
const handleSigint = () => child.kill();

process.on('SIGINT', handleSigint);

child.on('error', error => {
process.off('SIGINT', handleSigint);
fail(error);
});

child.on('close', (code, signal) => {
process.off('SIGINT', handleSigint);

if (code === 0) {
done();
} else {
fail(
new Error(signal ? `Process killed, signal ${signal}` : `Process exited, code ${code}`),
new Error(signal ? `Process closed, signal: ${signal}` : `Process exited, code: ${code}`),
);
}
});
Expand Down
51 changes: 51 additions & 0 deletions src/di/__test__/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ interface Calculator {
sum(a: number, b: number): number;
}

interface HttpClient {
request(method: string, resource: string): Promise<{ status: number }>;
}

describe('Container', () => {
test('basic behavior', () => {
const TOKEN = {
Expand Down Expand Up @@ -115,4 +119,51 @@ describe('Container', () => {
new Error('Cycle dependency found: Token(logger), Token(calculator), Token(logger)'),
);
});

test('should not throw circular dep error when dep used in two components', () => {
const TOKEN = {
main: createToken<VoidFunction>('main'),
logger: createToken<Logger>('logger'),
http: {
client: createToken<HttpClient>('httpClient'),
},
};

const container = createContainer();

container.set(TOKEN.main, resolve => {
const logger = resolve(TOKEN.logger);
const client = resolve(TOKEN.http.client);

return () => {
logger.info('App started');
client.request('POST', '/api/analytics?event=app_start');
};
});

container.set(TOKEN.http.client, resolve => {
const logger = resolve(TOKEN.logger);

return {
request() {
const status = 201;

logger.info(`incoming request done, status: ${status}`);

return Promise.resolve({ status });
},
};
});

container.set(TOKEN.logger, () => {
return {
info(message) {
// eslint-disable-next-line no-console
console.log(`msg: ${message}`);
},
};
});

expect(() => container.get(TOKEN.main)).not.toThrow();
});
});
9 changes: 9 additions & 0 deletions src/misc/__test__/timer-pool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, test } from 'node:test';
import { expect } from '@std/expect';
import { TimerPool } from '../timer-pool.ts';

describe('TimerPool', () => {
test('constructor should not fail in env without request/cancelAnimationFrame', () => {
expect(() => new TimerPool()).not.toThrow();
});
});
72 changes: 72 additions & 0 deletions src/react/di/__test__/use-dependency.web.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { type Mock, describe, mock, test } from 'node:test';
import { useEffect } from 'react';
import { expect } from '@std/expect';
import { render } from '@testing-library/react';
import { createContainer } from '../../../di/container.ts';
import { createToken } from '../../../di/token.ts';
import { ContainerProvider } from '../container-provider.tsx';
import { useDependency } from '../use-dependency.ts';

interface Logger {
info(message: string): void;
}

const TOKEN = {
logger: createToken<Logger>('logger'),
} as const;

describe('useDependency', () => {
const TestComponent = () => {
const logger = useDependency(TOKEN.logger);

useEffect(() => {
logger.info('Component mounted');
}, [logger]);

return <div>This is of useDependency</div>;
};

test('should return component from container', () => {
const container = createContainer();

container.set(TOKEN.logger, () => {
return {
info: mock.fn(),
};
});

const logger = container.get(TOKEN.logger);

expect((logger.info as Mock<Logger['info']>).mock.callCount()).toBe(0);

render(
<ContainerProvider container={container}>
<TestComponent />
</ContainerProvider>,
);

expect((logger.info as Mock<Logger['info']>).mock.callCount()).toBe(1);
});

test('should throw if component is not defined', () => {
const container = createContainer();

const mount = () => {
render(
<ContainerProvider container={container}>
<TestComponent />
</ContainerProvider>,
);
};

expect(mount).toThrow();
});

test('should throw if container is not provided', () => {
const mount = () => {
render(<TestComponent />);
};

expect(mount).toThrow();
});
});
Loading
Loading