@centreon/js-config 24.4.14 → 24.4.16

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,17 +4,28 @@ 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;
17
19
  }
20
+ interface Resolution {
21
+ height: number;
22
+ width: number;
23
+ }
24
+
25
+ interface MakeSnapshotWithCustomResolution {
26
+ resolution: Resolution;
27
+ title: string;
28
+ }
18
29
 
19
30
  export enum Method {
20
31
  DELETE = 'DELETE',
@@ -24,7 +35,7 @@ export enum Method {
24
35
  PUT = 'PUT'
25
36
  }
26
37
 
27
- Cypress.Commands.add('mount', ({ Component, options }) => {
38
+ Cypress.Commands.add('mount', ({ Component, options = {} }) => {
28
39
  const wrapped = (
29
40
  <ThemeProvider>
30
41
  <Box
@@ -36,11 +47,10 @@ Cypress.Commands.add('mount', ({ Component, options }) => {
36
47
  >
37
48
  {Component}
38
49
  </Box>
50
+ <CssBaseline />
39
51
  </ThemeProvider>
40
52
  );
41
53
 
42
- document.getElementsByTagName('body')[0].setAttribute('style', 'margin:0px');
43
-
44
54
  return mount(wrapped, options);
45
55
  });
46
56
 
@@ -95,20 +105,32 @@ Cypress.Commands.add(
95
105
  }
96
106
  );
97
107
 
98
- Cypress.Commands.add('moveSortableElement', ({ element, direction }): void => {
99
- const key = `{${direction}arrow}`;
108
+ interface MoveSortableElementProps {
109
+ direction: 'up' | 'down' | 'left' | 'right';
110
+ element: Cypress.Chainable<JQuery<HTMLElement>>;
111
+ times?: number;
112
+ }
100
113
 
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
- });
114
+ Cypress.Commands.add(
115
+ 'moveSortableElement',
116
+ ({ element, direction, times = 1 }: MoveSortableElementProps): void => {
117
+ const key = `{${direction}arrow}`;
118
+
119
+ element.type(' ', {
120
+ force: true,
121
+ scrollBehavior: false
122
+ });
123
+
124
+ Array.from({ length: times }).forEach(() => {
125
+ element.eq(-1).type(key, {
126
+ scrollBehavior: false
127
+ });
128
+ });
129
+ element.eq(-1).type(' ', {
130
+ scrollBehavior: false
131
+ });
132
+ }
133
+ );
112
134
 
113
135
  Cypress.Commands.add(
114
136
  'moveSortableElementUsingAriaLabel',
@@ -128,15 +150,49 @@ Cypress.Commands.add(
128
150
  }
129
151
  );
130
152
 
153
+ Cypress.Commands.add('adjustViewport', () => cy.viewport(1280, 590));
154
+
155
+ Cypress.Commands.add('makeSnapshot', (title?: string) => {
156
+ cy.adjustViewport();
157
+ cy.matchImageSnapshot(title);
158
+ });
159
+
160
+ Cypress.Commands.add(
161
+ 'makeSnapshotWithCustomResolution',
162
+ ({ title, resolution }: MakeSnapshotWithCustomResolution) => {
163
+ const { width, height } = resolution;
164
+ cy.viewport(width, height);
165
+ cy.matchImageSnapshot(title);
166
+ }
167
+ );
168
+
169
+ Cypress.Commands.add('cssDisableMotion', (): void => {
170
+ Cypress.on('window:before:load', (cyWindow) => {
171
+ disableMotion(cyWindow);
172
+ });
173
+ });
174
+
131
175
  declare global {
132
176
  namespace Cypress {
133
177
  interface Chainable {
178
+ adjustViewport: () => Cypress.Chainable;
179
+ cssDisableMotion: () => Cypress.Chainable;
180
+ getRequestCalls: (alias) => Cypress.Chainable;
134
181
  interceptAPIRequest: <T extends object>(
135
182
  props: InterceptAPIRequestProps<T>
136
183
  ) => Cypress.Chainable;
137
184
  interceptRequest: (method, path, mock, alias) => Cypress.Chainable;
138
- mount: ({ Component, options = {} }: MountProps) => Cypress.Chainable;
139
- moveSortableElement: ({ ariaLabel, direction }) => void;
185
+ makeSnapshot: (title?: string) => void;
186
+ makeSnapshotWithCustomResolution: ({
187
+ title,
188
+ resolution
189
+ }: MakeSnapshotWithCustomResolution) => Cypress.Chainable;
190
+ mount: ({ Component, options }: MountProps) => Cypress.Chainable;
191
+ moveSortableElement: ({
192
+ element,
193
+ direction,
194
+ times
195
+ }: MoveSortableElementProps) => void;
140
196
  moveSortableElementUsingAriaLabel: ({ ariaLabel, direction }) => void;
141
197
  waitForRequest: (alias) => Cypress.Chainable;
142
198
  }
@@ -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: {
@@ -47,6 +61,8 @@ module.exports = ({ webpackConfig, cypressFolder, specPattern, env }) => {
47
61
  reportFilename: '[name]-report.json'
48
62
  },
49
63
  video: true,
50
- videosFolder: `${mainCypressFolder}/results/videos`
64
+ videosFolder: `${mainCypressFolder}/results/videos`,
65
+ viewportHeight: 590,
66
+ viewportWidth: 1280
51
67
  });
52
68
  };
@@ -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
  };
@@ -144,6 +144,7 @@ interface Host {
144
144
  alias?: string | null;
145
145
  checkCommand?: string | null;
146
146
  checkPeriod?: string | null;
147
+ hostGroup?: string;
147
148
  maxCheckAttempts?: number | null;
148
149
  name: string;
149
150
  passiveCheckEnabled?: boolean;
@@ -159,6 +160,7 @@ Cypress.Commands.add(
159
160
  alias = null,
160
161
  checkCommand = null,
161
162
  checkPeriod = null,
163
+ hostGroup = '',
162
164
  maxCheckAttempts = 1,
163
165
  name,
164
166
  passiveCheckEnabled = true,
@@ -176,7 +178,7 @@ Cypress.Commands.add(
176
178
  bodyContent: {
177
179
  action: 'ADD',
178
180
  object: 'HOST',
179
- values: `${name};${hostAlias};${address};${template};${poller};`
181
+ values: `${name};${hostAlias};${address};${template};${poller};${hostGroup}`
180
182
  }
181
183
  })
182
184
  .then(() => {
@@ -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 {};