Skip to content

Commit a746d25

Browse files
committed
test: ✅ Ajoute des tests unitaires pour DsfrNavigation
1 parent 85b3748 commit a746d25

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed

src/components/DsfrNavigation/DsfrNavigationMegaMenu.js

Whitespace-only changes.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { render } from '@testing-library/vue'
2+
import { createRouter, createWebHistory } from 'vue-router'
3+
4+
import DsfrNavigationMegaMenu from './DsfrNavigationMegaMenu.vue'
5+
6+
const VIcon = { props: ['name'], template: '<i :class="name"></i>' }
7+
8+
const router = createRouter({
9+
history: createWebHistory('/'),
10+
routes: [{
11+
path: '/',
12+
name: 'Home',
13+
component: { template: '<div />' },
14+
}],
15+
})
16+
17+
describe('DsfrNavigationMegaMenu', () => {
18+
it('should render a navigation mega menu', async () => {
19+
const title = 'Titre du mega menu'
20+
const description = 'Description du mega menu'
21+
const link = {
22+
to: '/',
23+
text: 'Voir toute la rubrique',
24+
}
25+
const menus = [
26+
{
27+
title: 'Nom de catégorie',
28+
links: [
29+
{
30+
text: 'Lien 1',
31+
to: '/',
32+
},
33+
{
34+
text: 'Lien 2',
35+
to: '/',
36+
},
37+
{
38+
text: 'Lien 3',
39+
to: '/',
40+
},
41+
{
42+
text: 'Lien 4',
43+
to: '/',
44+
},
45+
{
46+
text: 'Lien 5',
47+
to: '/',
48+
},
49+
],
50+
},
51+
{
52+
title: 'Nom de catégorie',
53+
links: [
54+
{
55+
text: 'Lien 1',
56+
to: '/',
57+
},
58+
{
59+
text: 'Lien 2',
60+
to: '/',
61+
},
62+
{
63+
text: 'Lien 3',
64+
to: '/',
65+
},
66+
{
67+
text: 'Lien 4',
68+
to: '/',
69+
},
70+
{
71+
text: 'Lien 5',
72+
to: '/',
73+
},
74+
],
75+
},
76+
{
77+
title: 'Nom de catégorie',
78+
links: [
79+
{
80+
text: 'Lien 1',
81+
to: '/',
82+
},
83+
{
84+
text: 'Lien 2',
85+
to: '/',
86+
},
87+
{
88+
text: 'Lien 3',
89+
to: '/',
90+
},
91+
{
92+
text: 'Lien 4',
93+
to: '/',
94+
},
95+
{
96+
text: 'Lien 5',
97+
to: '/',
98+
},
99+
],
100+
},
101+
]
102+
103+
const { getByTestId, getByText, getAllByText } = render(DsfrNavigationMegaMenu, {
104+
global: {
105+
plugins: [router],
106+
components: {
107+
VIcon,
108+
},
109+
},
110+
props: {
111+
title,
112+
link,
113+
description,
114+
menus,
115+
},
116+
})
117+
118+
await router.isReady()
119+
120+
const [button, titleEl] = getAllByText(title)
121+
const descEl = getByText(description)
122+
const megaMenuWrapper = getByTestId('mega-menu-wrapper')
123+
124+
expect(button.tagName).toBe('BUTTON')
125+
expect(titleEl.tagName).toBe('H4')
126+
expect(megaMenuWrapper.id).toBe(button.getAttribute('aria-controls'))
127+
expect(descEl).toHaveClass('fr-text--sm')
128+
})
129+
})

src/components/DsfrNavigation/DsfrNavigationMegaMenu.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default {
5858
</button>
5959
<div
6060
:id="id"
61+
data-testid="mega-menu-wrapper"
6162
class="fr-collapse fr-mega-menu"
6263
tabindex="-1"
6364
:class="{ 'fr-collapse--expanded': expanded }"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { render } from '@testing-library/vue'
2+
import { createRouter, createWebHistory } from 'vue-router'
3+
4+
import DsfrNavigationMenu from './DsfrNavigationMenu.vue'
5+
6+
const VIcon = { props: ['name'], template: '<i :class="name"></i>' }
7+
8+
const router = createRouter({
9+
history: createWebHistory('/'),
10+
routes: [{
11+
path: '/',
12+
name: 'Home',
13+
component: { template: '<div />' },
14+
}],
15+
})
16+
17+
describe('DsfrNavigationMenu', () => {
18+
it('should render a navigation mega menu', async () => {
19+
const title = 'Titre du menu'
20+
const links = [
21+
{
22+
text: 'Lien 1',
23+
to: '/',
24+
},
25+
{
26+
text: 'Lien 2',
27+
to: '/',
28+
},
29+
{
30+
text: 'Lien 3',
31+
to: '/',
32+
},
33+
{
34+
text: 'Lien 4',
35+
to: '/',
36+
},
37+
{
38+
text: 'Lien 5',
39+
to: '/',
40+
},
41+
]
42+
43+
const { getByText, getByTestId, getAllByTestId } = render(DsfrNavigationMenu, {
44+
global: {
45+
plugins: [router],
46+
components: {
47+
VIcon,
48+
},
49+
},
50+
props: {
51+
title,
52+
links,
53+
},
54+
})
55+
56+
await router.isReady()
57+
58+
const button = getByText(title).parentElement
59+
const menuWrapper = getByTestId('navigation-menu')
60+
const linkEls = getAllByTestId('nav-router-link')
61+
62+
expect(menuWrapper.id).toBe(button.getAttribute('aria-controls'))
63+
expect(linkEls).toHaveLength(links.length)
64+
})
65+
})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { render } from '@testing-library/vue'
2+
import { createRouter, createWebHistory } from 'vue-router'
3+
4+
import DsfrNavigationMenuLink from './DsfrNavigationMenuLink.vue'
5+
6+
const VIcon = { props: ['name'], template: '<i :class="name"></i>' }
7+
8+
const router = createRouter({
9+
history: createWebHistory('/'),
10+
routes: [{
11+
path: '/',
12+
name: 'Home',
13+
component: { template: '<div />' },
14+
}],
15+
})
16+
17+
describe('DsfrNavigationMenuLink', () => {
18+
it('should render a navigation menu link (internal)', async () => {
19+
const to = '/'
20+
const text = 'Texte du lien'
21+
22+
const { getByTestId } = render(DsfrNavigationMenuLink, {
23+
global: {
24+
plugins: [router],
25+
components: {
26+
VIcon,
27+
},
28+
},
29+
props: {
30+
to,
31+
text,
32+
},
33+
})
34+
35+
await router.isReady()
36+
37+
const link = getByTestId('nav-router-link')
38+
expect(link.innerHTML).toBe(text)
39+
expect(link).toHaveAttribute('href', to)
40+
})
41+
})

src/components/DsfrNavigation/DsfrNavigationMenuLink.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default {
3333
<a
3434
v-if="isExternal"
3535
class="fr-nav__link"
36+
data-testid="nav-external-link"
3637
:href="to"
3738
@click="$emit('toggle-id', id)"
3839
>
@@ -41,6 +42,7 @@ export default {
4142
<router-link
4243
v-else
4344
class="fr-nav__link"
45+
data-testid="nav-router-link"
4446
:to="to"
4547
@click="$emit('toggle-id', id)"
4648
>

0 commit comments

Comments
 (0)