|
1 | | -import { expect, within } from 'storybook/test' |
| 1 | +import type { Meta, StoryObj } from '@storybook/vue3' |
| 2 | + |
| 3 | +import { expect, fn, within } from '@storybook/test' |
| 4 | +import { ref, watch } from 'vue' |
2 | 5 |
|
3 | 6 | import DsfrPagination from './DsfrPagination.vue' |
4 | 7 |
|
5 | 8 | /** |
6 | 9 | * [Voir quand l’utiliser sur la documentation du DSFR](https://www.systeme-de-design.gouv.fr/version-courante/fr/composants/pagination) |
7 | 10 | */ |
8 | | -export default { |
| 11 | +const meta = { |
9 | 12 | component: DsfrPagination, |
10 | 13 | title: 'Composants/DsfrPagination', |
11 | 14 | argTypes: { |
@@ -40,133 +43,130 @@ export default { |
40 | 43 | description: |
41 | 44 | 'Permet de limiter le nombre de pages affichées dans la pagination (avec un maximum de 5 pages)', |
42 | 45 | }, |
43 | | - 'update:currentPage': { |
44 | | - description: |
45 | | - 'Événement émis lors du changement de page courante, avec en argument le numéro de page sélectionné', |
46 | | - }, |
| 46 | + 'onUpdate:currentPage': fn(), |
47 | 47 | }, |
48 | | -} |
| 48 | +} satisfies Meta<typeof DsfrPagination> |
| 49 | + |
| 50 | +export default meta |
| 51 | +type Story = StoryObj<typeof meta> |
49 | 52 |
|
50 | | -export const Pagination = (args) => ({ |
| 53 | +const render = (args: any) => ({ |
51 | 54 | components: { |
52 | 55 | DsfrPagination, |
53 | 56 | }, |
54 | | - |
55 | | - data () { |
56 | | - return { ...args } |
| 57 | + setup () { |
| 58 | + const currentPage = ref(args.currentPage) |
| 59 | + watch(currentPage, (newPage) => { |
| 60 | + args['onUpdate:currentPage'](newPage) |
| 61 | + }) |
| 62 | + return { |
| 63 | + args, |
| 64 | + currentPage, |
| 65 | + } |
57 | 66 | }, |
58 | | - |
59 | 67 | template: ` |
60 | 68 | <DsfrPagination |
61 | | - :pages="pages" |
| 69 | + :pages="args.pages" |
62 | 70 | v-model:current-page="currentPage" |
63 | 71 | /> |
64 | 72 | `, |
65 | 73 | }) |
66 | | -Pagination.args = { |
67 | | - pages: [ |
68 | | - { |
69 | | - label: '1', |
70 | | - href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:0', |
71 | | - title: 'Page 1', |
72 | | - }, |
73 | | - { |
74 | | - label: '2', |
75 | | - href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:1', |
76 | | - title: 'Page 2', |
77 | | - }, |
78 | | - { |
79 | | - label: '3', |
80 | | - href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:2', |
81 | | - title: 'Page 3', |
82 | | - }, |
83 | | - { |
84 | | - label: '4', |
85 | | - href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:3', |
86 | | - title: 'Page 4', |
87 | | - }, |
88 | | - { |
89 | | - label: '5', |
90 | | - href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:4', |
91 | | - title: 'Page 5', |
92 | | - }, |
93 | | - ], |
94 | | - currentPage: 0, |
95 | | -} |
96 | | -Pagination.play = async ({ canvasElement }) => { |
97 | | - const canvas = within(canvasElement) |
98 | | - const links = canvas.getAllByRole('link') |
99 | | - expect(links).toHaveLength(9) |
100 | 74 |
|
101 | | - const currentPageLink = canvas.getByText(`${Pagination.args.currentPage + 1}`) |
102 | | - expect(currentPageLink).toHaveAttribute('aria-current', 'page') |
103 | | - const secondPageLink = canvas.getByText(`${Pagination.args.currentPage + 2}`) |
104 | | - expect(secondPageLink).not.toHaveAttribute('aria-current', 'page') |
105 | | -} |
106 | | - |
107 | | -export const PaginationTruncated = (args) => ({ |
108 | | - components: { |
109 | | - DsfrPagination, |
| 75 | +export const Pagination: Story = { |
| 76 | + render, |
| 77 | + args: { |
| 78 | + pages: [ |
| 79 | + { |
| 80 | + label: '1', |
| 81 | + href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:0', |
| 82 | + title: 'Page 1', |
| 83 | + }, |
| 84 | + { |
| 85 | + label: '2', |
| 86 | + href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:1', |
| 87 | + title: 'Page 2', |
| 88 | + }, |
| 89 | + { |
| 90 | + label: '3', |
| 91 | + href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:2', |
| 92 | + title: 'Page 3', |
| 93 | + }, |
| 94 | + { |
| 95 | + label: '4', |
| 96 | + href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:3', |
| 97 | + title: 'Page 4', |
| 98 | + }, |
| 99 | + { |
| 100 | + label: '5', |
| 101 | + href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:4', |
| 102 | + title: 'Page 5', |
| 103 | + }, |
| 104 | + ], |
| 105 | + currentPage: 0, |
110 | 106 | }, |
| 107 | + play: async ({ canvasElement, args }) => { |
| 108 | + const canvas = within(canvasElement) |
| 109 | + const links = canvas.getAllByRole('link') |
| 110 | + expect(links).toHaveLength(9) |
111 | 111 |
|
112 | | - data () { |
113 | | - return { ...args } |
| 112 | + const currentPageLink = canvas.getByText(`${(args.currentPage || 0) + 1}`) |
| 113 | + expect(currentPageLink).toHaveAttribute('aria-current', 'page') |
| 114 | + const secondPageLink = canvas.getByText(`${(args.currentPage || 0) + 2}`) |
| 115 | + expect(secondPageLink).not.toHaveAttribute('aria-current', 'page') |
114 | 116 | }, |
| 117 | +} |
115 | 118 |
|
116 | | - template: ` |
117 | | - <DsfrPagination |
118 | | - :pages="pages" |
119 | | - v-model:currentPage="currentPage" |
120 | | - /> |
121 | | - `, |
122 | | -}) |
123 | | -PaginationTruncated.args = { |
124 | | - pages: [ |
125 | | - { |
126 | | - label: '1', |
127 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:0', |
128 | | - title: 'Page 1', |
129 | | - }, |
130 | | - { |
131 | | - label: '2', |
132 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:1', |
133 | | - title: 'Page 2', |
134 | | - }, |
135 | | - { |
136 | | - label: '3', |
137 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:2', |
138 | | - title: 'Page 3', |
139 | | - }, |
140 | | - { |
141 | | - label: '4', |
142 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:3', |
143 | | - title: 'Page 4', |
144 | | - }, |
145 | | - { |
146 | | - label: '5', |
147 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:4', |
148 | | - title: 'Page 5', |
149 | | - }, |
150 | | - { |
151 | | - label: '6', |
152 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:5', |
153 | | - title: 'Page 6', |
154 | | - }, |
155 | | - { |
156 | | - label: '7', |
157 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:6', |
158 | | - title: 'Page 7', |
159 | | - }, |
160 | | - { |
161 | | - label: '8', |
162 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:7', |
163 | | - title: 'Page 8', |
164 | | - }, |
165 | | - { |
166 | | - label: '9', |
167 | | - href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:8', |
168 | | - title: 'Page 9', |
169 | | - }, |
170 | | - ], |
171 | | - currentPage: 4, |
| 119 | +export const PaginationTronquee: Story = { |
| 120 | + name: 'Pagination tronquée', |
| 121 | + render, |
| 122 | + args: { |
| 123 | + pages: [ |
| 124 | + { |
| 125 | + label: '1', |
| 126 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:0', |
| 127 | + title: 'Page 1', |
| 128 | + }, |
| 129 | + { |
| 130 | + label: '2', |
| 131 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:1', |
| 132 | + title: 'Page 2', |
| 133 | + }, |
| 134 | + { |
| 135 | + label: '3', |
| 136 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:2', |
| 137 | + title: 'Page 3', |
| 138 | + }, |
| 139 | + { |
| 140 | + label: '4', |
| 141 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:3', |
| 142 | + title: 'Page 4', |
| 143 | + }, |
| 144 | + { |
| 145 | + label: '5', |
| 146 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:4', |
| 147 | + title: 'Page 5', |
| 148 | + }, |
| 149 | + { |
| 150 | + label: '6', |
| 151 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:5', |
| 152 | + title: 'Page 6', |
| 153 | + }, |
| 154 | + { |
| 155 | + label: '7', |
| 156 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:6', |
| 157 | + title: 'Page 7', |
| 158 | + }, |
| 159 | + { |
| 160 | + label: '8', |
| 161 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:7', |
| 162 | + title: 'Page 8', |
| 163 | + }, |
| 164 | + { |
| 165 | + label: '9', |
| 166 | + href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:8', |
| 167 | + title: 'Page 9', |
| 168 | + }, |
| 169 | + ], |
| 170 | + currentPage: 4, |
| 171 | + }, |
172 | 172 | } |
0 commit comments