Skip to content

Commit

Permalink
test(module-loader): cover all exported function with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Heymdall committed Oct 11, 2023
1 parent 1fdd5d4 commit 3e8f5ee
Show file tree
Hide file tree
Showing 22 changed files with 1,232 additions and 64 deletions.
20 changes: 14 additions & 6 deletions packages/arui-scripts-modules/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ module.exports = {
root: true,
extends: ['custom/common'],
parserOptions: {
tsconfigRootDir: __dirname,
project: [
'./tsconfig.eslint.json'
],
},
};
tsconfigRootDir: __dirname,
project: [
'./tsconfig.eslint.json',
],
},
overrides: [
{
files: ['**/__tests__/**/*.{ts,tsx}'],
rules: {
'import/no-extraneous-dependencies': 'off',
},
}
],
};
1 change: 1 addition & 0 deletions packages/arui-scripts-modules/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
build
coverage
2 changes: 2 additions & 0 deletions packages/arui-scripts-modules/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
src
build/tsconfig.tsbuildinfo
__tests__
.turbo
coverage
4 changes: 4 additions & 0 deletions packages/arui-scripts-modules/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ module.exports = {
'/node_modules/',
'/build/',
],
collectCoverageFrom: [
'src/**/*.{ts,tsx}',
'!src/**/*.d.ts',
],
};
6 changes: 6 additions & 0 deletions packages/arui-scripts-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@
"react": ">16.18.0"
},
"devDependencies": {
"@testing-library/react-hooks": "^8.0.1",
"@types/react": "17.0.64",
"@types/uuid": "^9.0.5",
"eslint": "^8.20.0",
"eslint-config-custom": "workspace:*",
"jest": "28.1.3",
"jest-environment-jsdom": "^29.6.2",
"prettier": "^2.7.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"ts-jest": "28.0.8",
"typescript": "4.9.5"
},
"dependencies": {
"uuid": "^9.0.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { createModuleFetcher } from '../create-module-fetcher';
import { fetchAppManifest } from '../utils/fetch-app-manifest';

jest.mock('../utils/fetch-app-manifest');

describe('createModuleFetcher', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should fetch the app manifest and return module resources', async () => {
const mockManifest = {
__metadata__: {
version: '1.0',
name: 'Test App',
},
module1: {
js: 'module1.js',
css: 'module1.css',
mode: 'compat',
},
};

(fetchAppManifest as jest.Mock).mockResolvedValue(mockManifest);

const baseUrl = 'http://example.com';
const assetsUrl = '/assets/webpack-assets.json';
const moduleFetcher = createModuleFetcher({ baseUrl, assetsUrl });

const moduleId = 'module1';
const hostAppId = 'app1';

const expectedModuleResources = {
scripts: ['module1.js'],
styles: ['module1.css'],
moduleVersion: '1.0',
appName: 'Test App',
mountMode: 'compat',
moduleState: {
baseUrl: 'http://example.com',
hostAppId: 'app1',
},
};

const moduleResources = await moduleFetcher({ moduleId, hostAppId, params: undefined });

expect(fetchAppManifest).toHaveBeenCalledWith('http://example.com/assets/webpack-assets.json');
expect(moduleResources).toEqual(expectedModuleResources);
});

it('should throw an error if module is not found in the manifest', async () => {
const mockManifest = {
__metadata__: {
version: '1.0',
name: 'Test App',
},
};

(fetchAppManifest as jest.Mock).mockResolvedValue(mockManifest);

const baseUrl = 'http://example.com';
const assetsUrl = '/assets/webpack-assets.json';
const moduleFetcher = createModuleFetcher({ baseUrl, assetsUrl });

const moduleId = 'module1';
const hostAppId = 'app1';

await expect(moduleFetcher({ moduleId, hostAppId, params: undefined })).rejects.toThrow(
'Module module1 not found in manifest from http://example.com/assets/webpack-assets.json'
);

expect(fetchAppManifest).toHaveBeenCalledWith('http://example.com/assets/webpack-assets.json');
});
});
Loading

0 comments on commit 3e8f5ee

Please sign in to comment.