Skip to content

Commit 226d053

Browse files
Unit Test Support example
1 parent 68fa25c commit 226d053

36 files changed

+649
-0
lines changed

unit-test/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

unit-test/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Rsbuild / Create React App Example
2+
3+
This example demos a basic host application loading remote component.
4+
5+
- `host` is the host application (unit_test-based).
6+
- `remote` standalone application (unit_test-based) which exposes `Button` component.
7+
8+
# Running Demo
9+
10+
Run `pnpm run start`. This will build and serve both `host` and `remote` on ports 3001 and 3002 respectively.
11+
12+
- [localhost:3001](http://localhost:3000/) (HOST)
13+
- [localhost:3002](http://localhost:3002/) (STANDALONE REMOTE)
14+
15+
# Running Cypress E2E Tests
16+
17+
To run tests in interactive mode, run `npm run cypress:debug` from the root directory of the project. It will open Cypress Test Runner and allow to run tests in interactive mode. [More info about "How to run tests"](../../cypress/README.md#how-to-run-tests)
18+
19+
To build app and run test in headless mode, run `yarn e2e:ci`. It will build app and run tests for this workspace in headless mode. If tets failed cypress will create `cypress` directory in sample root folder with screenshots and videos.
20+
21+
["Best Practices, Rules amd more interesting information here](../../cypress/README.md)

unit-test/cypress.env.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"allure": true,
3+
"allureResultsPath": "../cypress-e2e/results/allure-results"
4+
}

unit-test/e2e/checkCraApps.cy.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { baseSelectors } from './../../cypress-e2e/common/selectors';
2+
import { BaseMethods } from '../../cypress-e2e/common/base';
3+
import { Constants } from '../../cypress-e2e/fixtures/constants';
4+
5+
const basePage: BaseMethods = new BaseMethods();
6+
7+
const appsData = [
8+
{
9+
appNameText: Constants.commonConstantsData.basicComponents.host,
10+
host: 3000,
11+
},
12+
{
13+
appNameText: Constants.commonConstantsData.basicComponents.remote,
14+
host: 3002,
15+
},
16+
];
17+
18+
appsData.forEach((property: { appNameText: string; host: number }) => {
19+
const appName = property.host === 3000 ? appsData[0].appNameText : appsData[1].appNameText;
20+
21+
describe('CRA', () => {
22+
context(`Check ${appName}`, () => {
23+
beforeEach(() => {
24+
basePage.openLocalhost({
25+
number: property.host,
26+
});
27+
});
28+
29+
it(`Check ${appName} elements exist on the page`, () => {
30+
basePage.checkElementWithTextPresence({
31+
selector: baseSelectors.tags.headers.h1,
32+
text: Constants.commonConstantsData.basicComponents.basicHostRemote,
33+
});
34+
basePage.checkElementWithTextPresence({
35+
selector: baseSelectors.tags.headers.h2,
36+
text: property.appNameText,
37+
});
38+
basePage.checkElementWithTextPresence({
39+
selector: baseSelectors.tags.coreElements.button,
40+
text: Constants.elementsText.craApp.buttonText,
41+
});
42+
});
43+
});
44+
});
45+
});

unit-test/host/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

unit-test/host/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

unit-test/host/__tests__/app.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import '@testing-library/jest-dom/extend-expect';
4+
import App from '@src/App.js';
5+
6+
describe('App Component', () => {
7+
beforeAll(async ()=>{
8+
await require('federation-test')
9+
});
10+
test('renders the main heading', () => {
11+
render(<App />);
12+
const mainHeading = screen.getByTestId('main-heading');
13+
expect(mainHeading).toBeInTheDocument();
14+
});
15+
16+
test('renders the subheading', () => {
17+
render(<App />);
18+
const subHeading = screen.getByTestId('sub-heading');
19+
expect(subHeading).toBeInTheDocument();
20+
});
21+
22+
test('renders the RemoteButton with fallback', async () => {
23+
render(<App />);
24+
const remoteButton = await screen.findByTestId('remote-button');
25+
expect(remoteButton).toBeInTheDocument();
26+
});
27+
});

unit-test/host/jest.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
moduleNameMapper: {
3+
'^@src/(.*)$': '<rootDir>/src/$1'
4+
},
5+
testEnvironment: 'jsdom',
6+
transform: {
7+
'^.+\\.jsx?$': 'babel-jest'
8+
},
9+
setupFiles: ['<rootDir>/jest.setup.js']
10+
};

unit-test/host/jest.setup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { setupFederationTest } = require('../mf-test');
2+
3+
module.exports = async () => {
4+
await setupFederationTest(require('./modulefederation.config'));
5+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { dependencies } = require('./package.json');
2+
3+
module.exports = {
4+
name: 'host',
5+
library: {type: 'commonjs-module', name: 'host'},
6+
remoteType: 'script',
7+
remotes: {
8+
remote: 'remote@http://localhost:3002/remoteEntry.js',
9+
},
10+
shared: {
11+
...dependencies,
12+
react: {
13+
singleton: true,
14+
requiredVersion: dependencies['react'],
15+
},
16+
'react-dom': {
17+
singleton: true,
18+
requiredVersion: dependencies['react-dom'],
19+
},
20+
},
21+
};

0 commit comments

Comments
 (0)