Skip to content

Commit

Permalink
fixing the path and the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taefi committed Apr 3, 2024
1 parent 0e0a1d6 commit d2fe861
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/ts/file-router/src/vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default function vitePluginFileSystemRouter({

const changeListener = (file: string): void => {
if (!file.startsWith(dir)) {
if (file.endsWith('/generated/file-routes.json')) {
if (file === fileURLToPath(runtimeUrls.json)) {
server.hot.send({ type: 'full-reload' });
}
return;
Expand Down
41 changes: 21 additions & 20 deletions packages/ts/file-router/test/vite-plugin/vite-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { use } from '@esm-bundle/chai';
import { EventEmitter } from 'node:events';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { expect, use } from '@esm-bundle/chai';
import chaiAsPromised from 'chai-as-promised';
// eslint-disable-next-line import/no-extraneous-dependencies
import { FSWatcher } from 'chokidar';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import vitePluginFileSystemRouter from '../../src/vite-plugin';
Expand All @@ -11,7 +11,11 @@ use(sinonChai);

describe('@vaadin/hilla-file-router', () => {
describe('vite-plugin', () => {
const watcher = new FSWatcher();
const rootDir = pathToFileURL('/path/to/project/');
const outDir = new URL('dist/', rootDir);
const viewsDir = new URL('frontend/views/', rootDir);
const generatedDir = new URL('frontend/generated/', rootDir);
const watcher = new EventEmitter();
const mockServer = {
hot: {
send: sinon.spy(),
Expand All @@ -20,39 +24,36 @@ describe('@vaadin/hilla-file-router', () => {
};
const plugin = vitePluginFileSystemRouter({ isDevMode: true });
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// @ts-expect-error: the configResolved method could be either a function or an object.
plugin.configResolved({
logger: { info: sinon.spy() },
root: '/path/to/project',
build: { outDir: '/path/to/project/dist' },
root: fileURLToPath(rootDir),
build: { outDir: fileURLToPath(outDir) },
});
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// @ts-expect-error: the configResolved method could be either a function or an object.
plugin.configureServer(mockServer);

beforeEach(() => {
sinon.resetHistory();
});

it('should send full-reload only when file-routes.json is added', () => {
sinon.assert.notCalled(mockServer.hot.send);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
mockServer.watcher.emit('add', '/path/to/generated/file-routes.json');
sinon.assert.calledWith(mockServer.hot.send, { type: 'full-reload' });
expect(mockServer.hot.send).to.not.be.called;
mockServer.watcher.emit('add', fileURLToPath(new URL('file-routes.json', generatedDir)));
expect(mockServer.hot.send).to.be.calledWith({ type: 'full-reload' });
});

it('should send full-reload only when file-routes.json changes', () => {
sinon.assert.notCalled(mockServer.hot.send);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
mockServer.watcher.emit('change', '/path/to/generated/file-routes.json');
sinon.assert.calledWith(mockServer.hot.send, { type: 'full-reload' });
expect(mockServer.hot.send).to.not.be.called;
mockServer.watcher.emit('change', fileURLToPath(new URL('file-routes.json', generatedDir)));
expect(mockServer.hot.send).to.be.calledWith({ type: 'full-reload' });
});

it('should not send full-reload when other files change', () => {
sinon.assert.notCalled(mockServer.hot.send);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
mockServer.watcher.emit('change', '/path/to/views/file.tsx');
sinon.assert.notCalled(mockServer.hot.send);
expect(mockServer.hot.send).to.not.be.called;
mockServer.watcher.emit('change', fileURLToPath(new URL('file.tsx', viewsDir)));
expect(mockServer.hot.send).to.not.be.called;
});
});
});

0 comments on commit d2fe861

Please sign in to comment.