Skip to content

Commit 753782d

Browse files
committed
maybe almost working ??
Signed-off-by: Antoine MAZEAS <[email protected]>
1 parent ff9187e commit 753782d

File tree

6 files changed

+101
-22
lines changed

6 files changed

+101
-22
lines changed

openbas-front/src/__tests__/components/common/ExportButton.test.tsx

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import ExportButton from '../../../components/common/ExportButton';
22
import { act, render } from '@testing-library/react'; // @testing-library/dom is needed as well as it is a peer dependency of @testing-library/react
33
import { describe, expect, it, vi } from 'vitest';
4-
import { getDefaultTags } from "../../fixtures/api-types.fixtures";
4+
import {createDefaultTags, createTagMap} from "../../fixtures/api-types.fixtures";
55
import { faker } from '@faker-js/faker';
6+
import TestRootComponent from "../../fixtures/TestRootComponent";
7+
import type { TagHelper as TagHelperType } from '../../../actions/helper';
8+
import {fetchTags } from '../../../actions/Tag'
9+
import React from "react";
10+
import {useAppDispatch} from "../../../utils/hooks";
11+
import useDataLoader from "../../../utils/hooks/useDataLoader";
12+
import {fetchPlatformParameters} from "../../../actions/Application";
13+
import { store } from '../../../store'
614

715
/* eslint-disable @typescript-eslint/no-explicit-any */
816
type testobj = { [key: string]: any };
@@ -15,28 +23,49 @@ function createObjWithDefaultKeys(objtype: string): testobj {
1523
}
1624

1725
describe('Generic export button', () => {
18-
const numberOfElements : number = 10;
19-
const numberOfTags : number = 5;
2026
const exportType: string = "testobj"
27+
const exportData: testobj[] = [
28+
createObjWithDefaultKeys(exportType),
29+
createObjWithDefaultKeys(exportType),
30+
createObjWithDefaultKeys(exportType)
31+
]
32+
const numberOfElements : number = exportData.length;
2133
const exportKeys = [
2234
`${exportType}_name`,
35+
`${exportType}_tags`,
2336
];
37+
const tags = createDefaultTags(5);
38+
const tagMap = createTagMap(tags);
39+
for(let obj of exportData) {
40+
obj[`${exportType}_tags`] = tags.map(t => t.tag_id);
41+
}
2442
vi.mock(
25-
'../../../../../../src/actions/Tag',
26-
() => ({ fetchTags: () => getDefaultTags(numberOfTags)})
43+
'../../../actions/Tag',
44+
() => ({fetchTags: () => tagMap})
2745
);
2846

29-
it("does something", () => {
30-
const { getByDisplayValue } = render(
31-
<ExportButton totalElements={numberOfElements} exportProps={{
32-
exportType: exportType,
33-
exportKeys: exportKeys,
34-
exportData: [createObjWithDefaultKeys(exportType)],
35-
exportFileName: "export"
36-
}} />,
47+
useAppDispatch()(fetchTags())
48+
49+
it("does something", async () => {
50+
const { getByRole } = render(
51+
<TestRootComponent>
52+
<ExportButton totalElements={numberOfElements} exportProps={{
53+
exportType: exportType,
54+
exportKeys: exportKeys,
55+
exportData: [createObjWithDefaultKeys(exportType)],
56+
exportFileName: "export.csv"
57+
}} />
58+
</TestRootComponent>,
3759
);
38-
act(() => {
39-
const firstname = getByDisplayValue("Export this list");
60+
await act(async () => {
61+
const firstname = getByRole("link");
62+
if (firstname.onclick) {
63+
const toto = await firstname.onclick(new MouseEvent("click"))
64+
console.log("here it is");
65+
console.log(firstname.href)
66+
console.log(firstname);
67+
}
68+
4069
expect(firstname).toBeDefined();
4170
});
4271
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {createContext} from "react";
2+
3+
const defaultValue = {}
4+
const TestContext = createContext(defaultValue)
5+
6+
export default TestContext;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {store} from "../../store";
2+
import React from 'react';
3+
import ConnectedIntlProvider from "../../components/AppIntlProvider";
4+
import ConnectedThemeProvider from "../../components/AppThemeProvider";
5+
import {CssBaseline} from "@mui/material";
6+
import { Provider } from 'react-redux';
7+
import TestContext from './TestContext'
8+
import {APP_BASE_PATH} from "../../utils/Action";
9+
import {BrowserRouter} from "react-router";
10+
11+
// @ts-ignore
12+
const TestRootComponent = ({children}) => {
13+
let component;
14+
return (<Provider store={store}>
15+
<BrowserRouter basename={"to"}>
16+
<TestContext.Provider value={{}}>
17+
<ConnectedIntlProvider>
18+
<ConnectedThemeProvider>
19+
<CssBaseline />
20+
{children}
21+
</ConnectedThemeProvider>
22+
</ConnectedIntlProvider>
23+
</TestContext.Provider>
24+
</BrowserRouter>
25+
</Provider>);
26+
};
27+
28+
export default TestRootComponent;

openbas-front/src/__tests__/fixtures/api-types.fixtures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { faker } from '@faker-js/faker';
22

33
import { Exercise, Organization, Scenario, Tag } from '../../utils/api-types';
44

5-
export function getDefaultTags(numberTags: number) : Tag[] {
5+
export function createDefaultTags(numberTags: number) : Tag[] {
66
return Array(numberTags).fill(null)
77
.map<Tag>((x): Tag => {
88
return { tag_id: faker.string.uuid(), tag_name: faker.lorem.sentence(), }
99
});
1010
}
1111

12-
export function createTagMap(numberTags: number): { [key: string]: Tag } {
12+
export function createTagMap(tags: Tag[]): { [key: string]: Tag } {
1313
const tagMap: { [key: string]: Tag } = {};
14-
for (let tag of getDefaultTags(numberTags)) {
14+
for (let tag of tags) {
1515
const id = tag.tag_id;
1616
tagMap[id] = tag;
1717
}

openbas-front/src/__tests__/utils/Environment.test.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
33

44
import { exportData } from '../../utils/Environment';
55
import {
6+
createDefaultTags,
67
createExercisesMap,
78
createOrganisationsMap,
89
createScenarioMap,
@@ -80,7 +81,7 @@ describe('exportData tests', () => {
8081
objtype,
8182
keys,
8283
[obj],
83-
createTagMap(3),
84+
createTagMap(createDefaultTags(3)),
8485
);
8586
const line = result[0];
8687
it('does not incorporate tags in line', () => {
@@ -90,7 +91,7 @@ describe('exportData tests', () => {
9091

9192
describe('when object has tags', () => {
9293
const obj = createObjWithDefaultKeys(objtype);
93-
const tagMap = createTagMap(3);
94+
const tagMap = createTagMap(createDefaultTags(3));
9495
obj[`${objtype}_tags`] = Object.keys(tagMap);
9596

9697
// the goal is to concatenate tag names in the export
@@ -122,7 +123,7 @@ describe('exportData tests', () => {
122123

123124
describe('when object has unknown tag', () => {
124125
const obj = createObjWithDefaultKeys(objtype);
125-
const tagMap = createTagMap(3);
126+
const tagMap = createTagMap(createDefaultTags(3));
126127
obj[`${objtype}_tags`] = [faker.string.uuid(), faker.string.uuid()]; // not found in tag map
127128

128129
// the goal is to concatenate tag names in the export

openbas-front/vitest.config.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@ import react from '@vitejs/plugin-react';
66
// eslint-disable-next-line @typescript-eslint/no-unused-vars
77
import jsdom from 'jsdom';
88
import { defineConfig } from 'vitest/config';
9+
import {transformWithEsbuild} from "vite";
910

1011
export default defineConfig({
11-
plugins: [react()],
12+
plugins: [
13+
{
14+
name: 'treat-js-files-as-jsx',
15+
async transform(code, id) {
16+
if (!id.match(/src\/.*\.js$/)) return null;
17+
// Use the exposed transform from vite, instead of directly
18+
// transforming with esbuild
19+
return transformWithEsbuild(code, id, {
20+
loader: 'jsx',
21+
jsx: 'automatic',
22+
});
23+
},
24+
},
25+
react()
26+
],
1227
test: {
1328
environment: 'jsdom',
1429
include: ['src/__tests__/**/**/*.test.{ts,tsx}'],

0 commit comments

Comments
 (0)