@centreon/js-config 23.10.64 → 23.10.66

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.
@@ -4,13 +4,15 @@ import React from 'react';
4
4
  import { mount } from 'cypress/react18';
5
5
  import { equals, isNil } from 'ramda';
6
6
 
7
- import { Box } from '@mui/material';
7
+ import { Box, CssBaseline } from '@mui/material';
8
8
 
9
9
  import { ThemeProvider } from '@centreon/ui';
10
10
 
11
11
  import '@testing-library/cypress/add-commands';
12
12
  import 'cypress-msw-interceptor';
13
13
 
14
+ import disableMotion from './disableCssTransitions';
15
+
14
16
  interface MountProps {
15
17
  Component: React.ReactNode;
16
18
  options?: object;
@@ -24,7 +26,7 @@ export enum Method {
24
26
  PUT = 'PUT'
25
27
  }
26
28
 
27
- Cypress.Commands.add('mount', ({ Component, options }) => {
29
+ Cypress.Commands.add('mount', ({ Component, options = {} }) => {
28
30
  const wrapped = (
29
31
  <ThemeProvider>
30
32
  <Box
@@ -36,11 +38,10 @@ Cypress.Commands.add('mount', ({ Component, options }) => {
36
38
  >
37
39
  {Component}
38
40
  </Box>
41
+ <CssBaseline />
39
42
  </ThemeProvider>
40
43
  );
41
44
 
42
- document.getElementsByTagName('body')[0].setAttribute('style', 'margin:0px');
43
-
44
45
  return mount(wrapped, options);
45
46
  });
46
47
 
@@ -95,20 +96,32 @@ Cypress.Commands.add(
95
96
  }
96
97
  );
97
98
 
98
- Cypress.Commands.add('moveSortableElement', ({ element, direction }): void => {
99
- const key = `{${direction}arrow}`;
99
+ interface MoveSortableElementProps {
100
+ direction: 'up' | 'down' | 'left' | 'right';
101
+ element: Cypress.Chainable<JQuery<HTMLElement>>;
102
+ times?: number;
103
+ }
100
104
 
101
- element.type(' ', {
102
- force: true,
103
- scrollBehavior: false
104
- });
105
- element.eq(-1).type(key, {
106
- scrollBehavior: false
107
- });
108
- element.eq(-1).type(' ', {
109
- scrollBehavior: false
110
- });
111
- });
105
+ Cypress.Commands.add(
106
+ 'moveSortableElement',
107
+ ({ element, direction, times = 1 }: MoveSortableElementProps): void => {
108
+ const key = `{${direction}arrow}`;
109
+
110
+ element.type(' ', {
111
+ force: true,
112
+ scrollBehavior: false
113
+ });
114
+
115
+ Array.from({ length: times }).forEach(() => {
116
+ element.eq(-1).type(key, {
117
+ scrollBehavior: false
118
+ });
119
+ });
120
+ element.eq(-1).type(' ', {
121
+ scrollBehavior: false
122
+ });
123
+ }
124
+ );
112
125
 
113
126
  Cypress.Commands.add(
114
127
  'moveSortableElementUsingAriaLabel',
@@ -128,15 +141,32 @@ Cypress.Commands.add(
128
141
  }
129
142
  );
130
143
 
144
+ Cypress.Commands.add('makeSnapshot', (title?: string) => {
145
+ cy.viewport(1280, 590);
146
+ cy.matchImageSnapshot(title);
147
+ });
148
+
149
+ Cypress.Commands.add('cssDisableMotion', (): void => {
150
+ Cypress.on('window:before:load', (cyWindow) => {
151
+ disableMotion(cyWindow);
152
+ });
153
+ });
154
+
131
155
  declare global {
132
156
  namespace Cypress {
133
157
  interface Chainable {
158
+ cssDisableMotion: () => Cypress.Chainable;
134
159
  interceptAPIRequest: <T extends object>(
135
160
  props: InterceptAPIRequestProps<T>
136
161
  ) => Cypress.Chainable;
137
162
  interceptRequest: (method, path, mock, alias) => Cypress.Chainable;
138
- mount: ({ Component, options = {} }: MountProps) => Cypress.Chainable;
139
- moveSortableElement: ({ ariaLabel, direction }) => void;
163
+ makeSnapshot: (title?: string) => void;
164
+ mount: ({ Component, options }: MountProps) => Cypress.Chainable;
165
+ moveSortableElement: ({
166
+ element,
167
+ direction,
168
+ times
169
+ }: MoveSortableElementProps) => void;
140
170
  moveSortableElementUsingAriaLabel: ({ ariaLabel, direction }) => void;
141
171
  waitForRequest: (alias) => Cypress.Chainable;
142
172
  }
@@ -3,29 +3,34 @@ const { defineConfig } = require('cypress');
3
3
  const {
4
4
  addMatchImageSnapshotPlugin
5
5
  } = require('@simonsmith/cypress-image-snapshot/plugin');
6
+ const cypressCodeCoverageTask = require('@cypress/code-coverage/task');
6
7
 
7
- module.exports = ({ webpackConfig, cypressFolder, specPattern, env }) => {
8
+ module.exports = ({
9
+ webpackConfig,
10
+ cypressFolder,
11
+ specPattern,
12
+ env,
13
+ useVite = false,
14
+ excludeSpecPattern
15
+ }) => {
8
16
  const mainCypressFolder = cypressFolder || 'cypress';
9
17
 
10
18
  return defineConfig({
11
19
  component: {
12
20
  devServer: {
13
- bundler: 'webpack',
21
+ bundler: useVite ? 'vite' : 'webpack',
14
22
  framework: 'react',
15
23
  webpackConfig
16
24
  },
25
+ excludeSpecPattern,
17
26
  setupNodeEvents: (on, config) => {
18
27
  addMatchImageSnapshotPlugin(on, config);
19
28
 
29
+ cypressCodeCoverageTask(on, config);
30
+
20
31
  on('before:browser:launch', (browser, launchOptions) => {
21
32
  if (browser.name === 'chrome' && browser.isHeadless) {
22
- launchOptions.args = launchOptions.args.map((arg) => {
23
- if (arg === '--headless') {
24
- return '--headless=new';
25
- }
26
-
27
- return arg;
28
- });
33
+ launchOptions.args.push('--headless=new');
29
34
  }
30
35
 
31
36
  return launchOptions;
@@ -35,8 +40,17 @@ module.exports = ({ webpackConfig, cypressFolder, specPattern, env }) => {
35
40
  supportFile: `${mainCypressFolder}/support/component.tsx`
36
41
  },
37
42
  env: {
38
- ...env,
39
- baseUrl: 'http://localhost:9092'
43
+ baseUrl: 'http://localhost:9092',
44
+ codeCoverage: {
45
+ exclude: [
46
+ 'cypress/**/*.*',
47
+ 'packages/**',
48
+ 'node_modules',
49
+ '**/*.js',
50
+ '**/*.spec.tsx'
51
+ ]
52
+ },
53
+ ...env
40
54
  },
41
55
  reporter: 'mochawesome',
42
56
  reporterOptions: {
@@ -0,0 +1,19 @@
1
+ const disableMotion = (win): void => {
2
+ const injectedStyleEl = win.document.getElementById('__cy_disable_motion__');
3
+ if (injectedStyleEl) {
4
+ return;
5
+ }
6
+ win.document.head.insertAdjacentHTML(
7
+ 'beforeend',
8
+ `
9
+ <style id="__cy_disable_motion__">
10
+ /* Disable CSS transitions. */
11
+ *, *::before, *::after { -webkit-transition: none !important; -moz-transition: none !important; -o-transition: none !important; -ms-transition: none !important; transition: none !important; }
12
+ /* Disable CSS animations. */
13
+ *, *::before, *::after { -webkit-animation: none !important; -moz-animation: none !important; -o-animation: none !important; -ms-animation: none !important; animation: none !important; }
14
+ </style>
15
+ `.trim()
16
+ );
17
+ };
18
+
19
+ export default disableMotion;
@@ -13,7 +13,7 @@ const enableVisualTesting = (cypressFolder = 'cypress'): void => {
13
13
  capture: 'viewport',
14
14
  customDiffConfig: { threshold: 0.01 },
15
15
  customSnapshotsDir: `${cypressFolder}/visual-testing-snapshots`,
16
- failureThreshold: 0.06,
16
+ failureThreshold: 0.07,
17
17
  failureThresholdType: 'percent'
18
18
  });
19
19
  };
@@ -0,0 +1,75 @@
1
+ /* eslint-disable @typescript-eslint/no-namespace */
2
+
3
+ const apiBase = '/centreon/api';
4
+ const apiActionV1 = `${apiBase}/index.php`;
5
+
6
+ const getStatusNumberFromString = (status: string): number => {
7
+ const statuses = {
8
+ critical: '2',
9
+ down: '1',
10
+ ok: '0',
11
+ unknown: '3',
12
+ unreachable: '2',
13
+ up: '0',
14
+ warning: '1'
15
+ };
16
+
17
+ if (status in statuses) {
18
+ return statuses[status];
19
+ }
20
+
21
+ throw new Error(`Status ${status} does not exist`);
22
+ };
23
+
24
+ interface SubmitResult {
25
+ host: string;
26
+ output: string;
27
+ perfdata?: string | null;
28
+ service?: string | null;
29
+ status: string;
30
+ }
31
+
32
+ Cypress.Commands.add(
33
+ 'submitResults',
34
+ (results: Array<SubmitResult>): Cypress.Chainable => {
35
+ results.forEach(
36
+ ({ host, output, perfdata = '', service = null, status }) => {
37
+ const timestampNow = Math.floor(Date.now() / 1000) - 15;
38
+ const updatetime = timestampNow.toString();
39
+
40
+ const result = {
41
+ host,
42
+ output,
43
+ perfdata,
44
+ service,
45
+ status: getStatusNumberFromString(status),
46
+ updatetime
47
+ };
48
+
49
+ cy.request({
50
+ body: {
51
+ results: [result]
52
+ },
53
+ headers: {
54
+ 'Content-Type': 'application/json',
55
+ 'centreon-auth-token': window.localStorage.getItem('userTokenApiV1')
56
+ },
57
+ method: 'POST',
58
+ url: `${apiActionV1}?action=submit&object=centreon_submit_results`
59
+ });
60
+ }
61
+ );
62
+
63
+ return cy.wrap(null);
64
+ }
65
+ );
66
+
67
+ declare global {
68
+ namespace Cypress {
69
+ interface Chainable {
70
+ submitResults: (props: Array<SubmitResult>) => Cypress.Chainable;
71
+ }
72
+ }
73
+ }
74
+
75
+ export {};