@centreon/js-config 23.10.65 → 24.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,158 +1,203 @@
1
- /* eslint-disable @typescript-eslint/no-namespace */
2
- import React from 'react';
3
-
4
- import { mount } from 'cypress/react18';
5
- import { equals, isNil } from 'ramda';
6
-
7
- import { Box, CssBaseline } from '@mui/material';
8
-
9
- import { ThemeProvider } from '@centreon/ui';
10
-
11
- import '@testing-library/cypress/add-commands';
12
- import 'cypress-msw-interceptor';
13
-
14
- import disableMotion from './disableCssTransitions';
15
-
16
- interface MountProps {
17
- Component: React.ReactNode;
18
- options?: object;
19
- }
20
-
21
- export enum Method {
22
- DELETE = 'DELETE',
23
- GET = 'GET',
24
- PATCH = 'PATCH',
25
- POST = 'POST',
26
- PUT = 'PUT'
27
- }
28
-
29
- Cypress.Commands.add('mount', ({ Component, options = {} }) => {
30
- const wrapped = (
31
- <ThemeProvider>
32
- <Box
33
- sx={{
34
- backgroundColor: 'background.paper',
35
- height: '100%',
36
- width: '100%'
37
- }}
38
- >
39
- {Component}
40
- </Box>
41
- <CssBaseline />
42
- </ThemeProvider>
43
- );
44
-
45
- return mount(wrapped, options);
46
- });
47
-
48
- interface Query {
49
- name: string;
50
- value: string;
51
- }
52
-
53
- export interface InterceptAPIRequestProps<T> {
54
- alias: string;
55
- method: Method;
56
- path: string;
57
- query?: Query;
58
- response?: T | Array<T>;
59
- statusCode?: number;
60
- }
61
-
62
- Cypress.Commands.add(
63
- 'interceptAPIRequest',
64
- <T extends object>({
65
- method,
66
- path,
67
- response,
68
- alias,
69
- query,
70
- statusCode = 200
71
- }: InterceptAPIRequestProps<T>): void => {
72
- cy.interceptRequest(
73
- method,
74
- path.replace('./', '**'),
75
- (req, res, ctx) => {
76
- const getQuery = req?.url?.searchParams?.get(query?.name);
77
- if (query && equals(query.value, getQuery)) {
78
- return res(
79
- ctx.delay(500),
80
- ctx.json(response),
81
- ctx.status(statusCode)
82
- );
83
- }
84
- if (!getQuery && isNil(query)) {
85
- return res(
86
- ctx.delay(500),
87
- ctx.json(response),
88
- ctx.status(statusCode)
89
- );
90
- }
91
-
92
- return null;
93
- },
94
- alias
95
- );
96
- }
97
- );
98
-
99
- Cypress.Commands.add('moveSortableElement', ({ element, direction }): void => {
100
- const key = `{${direction}arrow}`;
101
-
102
- element.type(' ', {
103
- force: true,
104
- scrollBehavior: false
105
- });
106
- element.eq(-1).type(key, {
107
- scrollBehavior: false
108
- });
109
- element.eq(-1).type(' ', {
110
- scrollBehavior: false
111
- });
112
- });
113
-
114
- Cypress.Commands.add(
115
- 'moveSortableElementUsingAriaLabel',
116
- ({ ariaLabel, direction }): void => {
117
- const key = `{${direction}arrow}`;
118
-
119
- cy.findByLabelText(ariaLabel).type(' ', {
120
- force: true,
121
- scrollBehavior: false
122
- });
123
- cy.findAllByLabelText(ariaLabel).eq(-1).type(key, {
124
- scrollBehavior: false
125
- });
126
- cy.findAllByLabelText(ariaLabel).eq(-1).type(' ', {
127
- scrollBehavior: false
128
- });
129
- }
130
- );
131
-
132
- Cypress.Commands.add('makeSnapshot', (title?: string) => {
133
- cy.viewport(1280, 590);
134
- cy.matchImageSnapshot(title);
135
- });
136
-
137
- Cypress.Commands.add('cssDisableMotion', (): void => {
138
- Cypress.on('window:before:load', (cyWindow) => {
139
- disableMotion(cyWindow);
140
- });
141
- });
142
-
143
- declare global {
144
- namespace Cypress {
145
- interface Chainable {
146
- cssDisableMotion: () => Cypress.Chainable;
147
- interceptAPIRequest: <T extends object>(
148
- props: InterceptAPIRequestProps<T>
149
- ) => Cypress.Chainable;
150
- interceptRequest: (method, path, mock, alias) => Cypress.Chainable;
151
- makeSnapshot: (title?: string) => void;
152
- mount: ({ Component, options }: MountProps) => Cypress.Chainable;
153
- moveSortableElement: ({ element, direction }) => void;
154
- moveSortableElementUsingAriaLabel: ({ ariaLabel, direction }) => void;
155
- waitForRequest: (alias) => Cypress.Chainable;
156
- }
157
- }
158
- }
1
+ /* eslint-disable @typescript-eslint/no-namespace */
2
+ import React from 'react';
3
+
4
+ import { mount } from 'cypress/react18';
5
+ import { equals, isNil } from 'ramda';
6
+
7
+ import { Box, CssBaseline } from '@mui/material';
8
+
9
+ import { ThemeProvider } from '@centreon/ui';
10
+
11
+ import '@testing-library/cypress/add-commands';
12
+ import 'cypress-msw-interceptor';
13
+ import 'cypress-real-events';
14
+
15
+ import disableMotion from './disableCssTransitions';
16
+
17
+ interface MountProps {
18
+ Component: React.ReactNode;
19
+ options?: object;
20
+ }
21
+ interface Resolution {
22
+ height: number;
23
+ width: number;
24
+ }
25
+
26
+ interface MakeSnapshotWithCustomResolution {
27
+ resolution: Resolution;
28
+ title: string;
29
+ }
30
+
31
+ export enum Method {
32
+ DELETE = 'DELETE',
33
+ GET = 'GET',
34
+ PATCH = 'PATCH',
35
+ POST = 'POST',
36
+ PUT = 'PUT'
37
+ }
38
+
39
+ Cypress.Commands.add('mount', ({ Component, options = {} }) => {
40
+ const wrapped = (
41
+ <ThemeProvider>
42
+ <Box
43
+ sx={{
44
+ backgroundColor: 'background.paper',
45
+ height: '100%',
46
+ width: '100%'
47
+ }}
48
+ >
49
+ {Component}
50
+ </Box>
51
+ <CssBaseline />
52
+ </ThemeProvider>
53
+ );
54
+
55
+ return mount(wrapped, options);
56
+ });
57
+
58
+ interface Query {
59
+ name: string;
60
+ value: string;
61
+ }
62
+
63
+ export interface InterceptAPIRequestProps<T> {
64
+ alias: string;
65
+ delay?: number;
66
+ method: Method;
67
+ path: string;
68
+ query?: Query;
69
+ response?: T | Array<T>;
70
+ statusCode?: number;
71
+ }
72
+
73
+ Cypress.Commands.add(
74
+ 'interceptAPIRequest',
75
+ <T extends object>({
76
+ method,
77
+ path,
78
+ response,
79
+ alias,
80
+ query,
81
+ statusCode = 200,
82
+ delay = 500
83
+ }: InterceptAPIRequestProps<T>): void => {
84
+ cy.interceptRequest(
85
+ method,
86
+ path.replace('./', '**'),
87
+ (req, res, ctx) => {
88
+ const getQuery = req?.url?.searchParams?.get(query?.name);
89
+ if (query && equals(query.value, getQuery)) {
90
+ return res(
91
+ ctx.delay(delay),
92
+ ctx.json(response),
93
+ ctx.status(statusCode)
94
+ );
95
+ }
96
+ if (!getQuery && isNil(query)) {
97
+ return res(
98
+ ctx.delay(delay),
99
+ ctx.json(response),
100
+ ctx.status(statusCode)
101
+ );
102
+ }
103
+
104
+ return null;
105
+ },
106
+ alias
107
+ );
108
+ }
109
+ );
110
+
111
+ interface MoveSortableElementProps {
112
+ direction: 'up' | 'down' | 'left' | 'right';
113
+ element: Cypress.Chainable<JQuery<HTMLElement>>;
114
+ times?: number;
115
+ }
116
+
117
+ Cypress.Commands.add(
118
+ 'moveSortableElement',
119
+ ({ element, direction, times = 1 }: MoveSortableElementProps): void => {
120
+ const key = `{${direction}arrow}`;
121
+
122
+ element.type(' ', {
123
+ force: true,
124
+ scrollBehavior: false
125
+ });
126
+
127
+ Array.from({ length: times }).forEach(() => {
128
+ element.eq(-1).type(key, {
129
+ scrollBehavior: false
130
+ });
131
+ });
132
+ element.eq(-1).type(' ', {
133
+ scrollBehavior: false
134
+ });
135
+ }
136
+ );
137
+
138
+ Cypress.Commands.add(
139
+ 'moveSortableElementUsingAriaLabel',
140
+ ({ ariaLabel, direction }): void => {
141
+ const key = `{${direction}arrow}`;
142
+
143
+ cy.findByLabelText(ariaLabel).type(' ', {
144
+ force: true,
145
+ scrollBehavior: false
146
+ });
147
+ cy.findAllByLabelText(ariaLabel).eq(-1).type(key, {
148
+ scrollBehavior: false
149
+ });
150
+ cy.findAllByLabelText(ariaLabel).eq(-1).type(' ', {
151
+ scrollBehavior: false
152
+ });
153
+ }
154
+ );
155
+
156
+ Cypress.Commands.add('adjustViewport', () => cy.viewport(1280, 590));
157
+
158
+ Cypress.Commands.add('makeSnapshot', (title?: string) => {
159
+ cy.adjustViewport();
160
+ cy.matchImageSnapshot(title);
161
+ });
162
+
163
+ Cypress.Commands.add(
164
+ 'makeSnapshotWithCustomResolution',
165
+ ({ title, resolution }: MakeSnapshotWithCustomResolution) => {
166
+ const { width, height } = resolution;
167
+ cy.viewport(width, height);
168
+ cy.matchImageSnapshot(title);
169
+ }
170
+ );
171
+
172
+ Cypress.Commands.add('cssDisableMotion', (): void => {
173
+ Cypress.on('window:before:load', (cyWindow) => {
174
+ disableMotion(cyWindow);
175
+ });
176
+ });
177
+
178
+ declare global {
179
+ namespace Cypress {
180
+ interface Chainable {
181
+ adjustViewport: () => Cypress.Chainable;
182
+ cssDisableMotion: () => Cypress.Chainable;
183
+ getRequestCalls: (alias) => Cypress.Chainable;
184
+ interceptAPIRequest: <T extends object>(
185
+ props: InterceptAPIRequestProps<T>
186
+ ) => Cypress.Chainable;
187
+ interceptRequest: (method, path, mock, alias) => Cypress.Chainable;
188
+ makeSnapshot: (title?: string) => void;
189
+ makeSnapshotWithCustomResolution: ({
190
+ title,
191
+ resolution
192
+ }: MakeSnapshotWithCustomResolution) => Cypress.Chainable;
193
+ mount: ({ Component, options }: MountProps) => Cypress.Chainable;
194
+ moveSortableElement: ({
195
+ element,
196
+ direction,
197
+ times
198
+ }: MoveSortableElementProps) => void;
199
+ moveSortableElementUsingAriaLabel: ({ ariaLabel, direction }) => void;
200
+ waitForRequest: (alias) => Cypress.Chainable;
201
+ }
202
+ }
203
+ }
@@ -1,54 +1,83 @@
1
- /* eslint-disable @typescript-eslint/no-var-requires */
2
- const { defineConfig } = require('cypress');
3
- const {
4
- addMatchImageSnapshotPlugin
5
- } = require('@simonsmith/cypress-image-snapshot/plugin');
6
-
7
- module.exports = ({
8
- webpackConfig,
9
- cypressFolder,
10
- specPattern,
11
- env,
12
- useVite = false,
13
- excludeSpecPattern
14
- }) => {
15
- const mainCypressFolder = cypressFolder || 'cypress';
16
-
17
- return defineConfig({
18
- component: {
19
- devServer: {
20
- bundler: useVite ? 'vite' : 'webpack',
21
- framework: 'react',
22
- webpackConfig
23
- },
24
- excludeSpecPattern,
25
- setupNodeEvents: (on, config) => {
26
- addMatchImageSnapshotPlugin(on, config);
27
-
28
- on('before:browser:launch', (browser, launchOptions) => {
29
- if (browser.name === 'chrome' && browser.isHeadless) {
30
- launchOptions.args.push('--headless=new');
31
- }
32
-
33
- return launchOptions;
34
- });
35
- },
36
- specPattern,
37
- supportFile: `${mainCypressFolder}/support/component.tsx`
38
- },
39
- env: {
40
- ...env,
41
- baseUrl: 'http://localhost:9092'
42
- },
43
- reporter: 'mochawesome',
44
- reporterOptions: {
45
- html: false,
46
- json: true,
47
- overwrite: true,
48
- reportDir: `${mainCypressFolder}/results`,
49
- reportFilename: '[name]-report.json'
50
- },
51
- video: true,
52
- videosFolder: `${mainCypressFolder}/results/videos`
53
- });
54
- };
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ const { defineConfig } = require('cypress');
3
+ const { devServer } = require('cypress-rspack-dev-server');
4
+ const {
5
+ addMatchImageSnapshotPlugin
6
+ } = require('@simonsmith/cypress-image-snapshot/plugin');
7
+ const cypressCodeCoverageTask = require('@cypress/code-coverage/task');
8
+
9
+ module.exports = ({
10
+ webpackConfig,
11
+ cypressFolder,
12
+ specPattern,
13
+ env,
14
+ useVite = false,
15
+ excludeSpecPattern
16
+ }) => {
17
+ const mainCypressFolder = cypressFolder || 'cypress';
18
+
19
+ return defineConfig({
20
+ component: {
21
+ devServer: useVite
22
+ ? {
23
+ bundler: 'vite',
24
+ framework: 'react',
25
+ webpackConfig
26
+ }
27
+ : (devServerConfig) =>
28
+ devServer({
29
+ ...devServerConfig,
30
+ framework: 'react',
31
+ rspackConfig: webpackConfig
32
+ }),
33
+ excludeSpecPattern,
34
+ setupNodeEvents: (on, config) => {
35
+ addMatchImageSnapshotPlugin(on, config);
36
+
37
+ cypressCodeCoverageTask(on, config);
38
+ on('task', {
39
+ coverageReport: () => {
40
+ return null;
41
+ }
42
+ });
43
+
44
+ on('before:browser:launch', (browser, launchOptions) => {
45
+ if (browser.name === 'chrome' && browser.isHeadless) {
46
+ launchOptions.args.push('--headless=new');
47
+ launchOptions.args.push('--force-color-profile=srgb');
48
+ launchOptions.args.push('--window-size=1400,1200');
49
+ }
50
+
51
+ return launchOptions;
52
+ });
53
+ },
54
+ specPattern,
55
+ supportFile: `${mainCypressFolder}/support/component.tsx`
56
+ },
57
+ env: {
58
+ baseUrl: 'http://localhost:9092',
59
+ codeCoverage: {
60
+ exclude: [
61
+ 'cypress/**/*.*',
62
+ 'packages/**',
63
+ 'node_modules',
64
+ '**/*.js',
65
+ '**/*.spec.tsx'
66
+ ]
67
+ },
68
+ ...env
69
+ },
70
+ reporter: 'mochawesome',
71
+ reporterOptions: {
72
+ html: false,
73
+ json: true,
74
+ overwrite: true,
75
+ reportDir: `${mainCypressFolder}/results`,
76
+ reportFilename: '[name]-report.json'
77
+ },
78
+ video: true,
79
+ videosFolder: `${mainCypressFolder}/results/videos`,
80
+ viewportHeight: 590,
81
+ viewportWidth: 1280
82
+ });
83
+ };
@@ -1,21 +1,21 @@
1
- import { addMatchImageSnapshotCommand } from '@simonsmith/cypress-image-snapshot/command';
2
-
3
- const enableVisualTesting = (cypressFolder = 'cypress'): void => {
4
- if (Cypress.config('isInteractive')) {
5
- Cypress.Commands.add('matchImageSnapshot', () => {
6
- cy.log('Skipping snapshot');
7
- });
8
-
9
- return;
10
- }
11
-
12
- addMatchImageSnapshotCommand({
13
- capture: 'viewport',
14
- customDiffConfig: { threshold: 0.01 },
15
- customSnapshotsDir: `${cypressFolder}/visual-testing-snapshots`,
16
- failureThreshold: 0.07,
17
- failureThresholdType: 'percent'
18
- });
19
- };
20
-
21
- export default enableVisualTesting;
1
+ import { addMatchImageSnapshotCommand } from '@simonsmith/cypress-image-snapshot/command';
2
+
3
+ const enableVisualTesting = (cypressFolder = 'cypress'): void => {
4
+ if (Cypress.config('isInteractive')) {
5
+ Cypress.Commands.add('matchImageSnapshot', () => {
6
+ cy.log('Skipping snapshot');
7
+ });
8
+
9
+ return;
10
+ }
11
+
12
+ addMatchImageSnapshotCommand({
13
+ capture: 'viewport',
14
+ customDiffConfig: { threshold: 0.01 },
15
+ customSnapshotsDir: `${cypressFolder}/visual-testing-snapshots`,
16
+ failureThreshold: 0.07,
17
+ failureThresholdType: 'percent'
18
+ });
19
+ };
20
+
21
+ export default enableVisualTesting;
@@ -0,0 +1,36 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const filePath = process.argv[2];
5
+
6
+ const { error: logError } = console;
7
+
8
+ try {
9
+ const outFile = fs.readFileSync(path.resolve(filePath)).toString();
10
+ const outFileJson = JSON.parse(outFile);
11
+
12
+ const coveragesWithoutNodeModules = Object.entries(outFileJson)
13
+ .map(([key, value]) => {
14
+ if (key.includes('node_modules')) {
15
+ return undefined;
16
+ }
17
+
18
+ return [key, value];
19
+ })
20
+ .filter((v) => v);
21
+
22
+ const finalOutJson = coveragesWithoutNodeModules.reduce(
23
+ (acc, [key, value]) => ({
24
+ ...acc,
25
+ [key]: value
26
+ }),
27
+ {}
28
+ );
29
+
30
+ fs.writeFileSync(
31
+ path.resolve(filePath),
32
+ JSON.stringify(finalOutJson, null, 2)
33
+ );
34
+ } catch (error) {
35
+ logError(error.message);
36
+ }