Skip to content

Commit 4c09ce5

Browse files
committed
Tests: Add test for tabs
1 parent da887f4 commit 4c09ce5

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

exampleSite/content/test-product/tab-group/tab-group.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ weight: 200
66

77
## tab-group
88

9+
<div data-testid="tabs-test__basic">
910
{{<tabs name="common_steps_for_nginx_oss_and_plus">}}
1011

1112
{{%tab name="Call Outs"%}}
@@ -65,6 +66,7 @@ nginx -s reload
6566

6667

6768
{{</tabs>}}
69+
</div>
6870

6971

7072
## Much nested tab group

layouts/shortcodes/tabs.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
{{- $tab_set_id := .Get "name" | default (printf "tabset-%s-%d" (.Page.RelPermalink) (.Page.Scratch.Get "tabset-counter") ) | anchorize -}}
33
{{- $tabs := .Scratch.Get "tabs" -}}
44
{{- if .Inner -}}
5-
<div class="tabs-container" id="{{ $tab_set_id }}">
5+
<div class="tabs-container" id="{{ $tab_set_id }}" data-testid="tabs-container">
66
{{- range $i, $e := $tabs -}}
7-
{{- $id := printf "%s-tab%d" $tab_set_id $i -}}
7+
{{- $id := printf "%s-tab-%d" $tab_set_id $i -}}
88
{{- if eq $i 0 -}}
99
<input type="radio" name="{{$tab_set_id}}" id="{{$id}}" aria-hidden="true" checked="checked">
1010
{{- else -}}
1111
<input type="radio" name="{{$tab_set_id}}" id="{{$id}}" aria-hidden="true">
1212
{{- end -}}
1313
{{- end -}}
14-
<ul class="tab-labels" role="tablist" id="{{ $tab_set_id }}" data-testid="labels-{{ $tab_set_id }}">
14+
<ul class="tab-labels" role="tablist" id="{{ $tab_set_id }}" data-testid="tab-labels">
1515
{{- range $i, $e := $tabs -}}
16-
{{- $id := printf "%s-tab%d" $tab_set_id $i -}}
16+
{{- $id := printf "%s-tab-%d" $tab_set_id $i -}}
1717
<li>
1818
{{- if eq $i 0 -}}
1919
<label class="tab-label" role="tab" id="{{$id}}" for="{{$id}}" aria-controls="{{ $id }}" aria-selected="true" tabindex="0" data-testid="tab-label">
@@ -27,10 +27,10 @@
2727
</li>
2828
{{- end -}}
2929
</ul>
30-
<div class="tab-contents" data-testid="contents-{{ $tab_set_id }}">
30+
<div class="tab-contents" data-testid="tab-contents">
3131
{{- range $i, $e := $tabs -}}
3232
{{- $id := printf "%s-panel%d" $tab_set_id $i -}}
33-
<div class="tab-content" role="tabpanel" id="{{ $tab_set_id }}-panel{{ $i }}" aria-labelledby="{{ $id }}">
33+
<div class="tab-content" role="tabpanel" id="{{ $tab_set_id }}-panel-{{ $i }}" aria-labelledby="{{ $id }}" data-testid="tab-content">
3434
{{ with .content }}
3535
{{ . }}
3636
{{ end }}

tests/src/tabs.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { expect, test } from "@playwright/test";
2+
import { handleConsentPopup, waitFor } from "./util";
3+
4+
test.describe("Testing for tabs shortcode", () => {
5+
test.beforeEach(async ({ page }) => {
6+
const tabsUrl = "test-product/tab-group/tab-group/";
7+
await page.goto(`/${tabsUrl}`);
8+
await waitFor(async () => await handleConsentPopup(page));
9+
});
10+
11+
test("tabs render", async ({ page }) => {
12+
const section = await page.getByTestId("tabs-test__basic");
13+
const tabsContainer = await section.getByTestId("tabs-container");
14+
const tabLabels = await tabsContainer.getByTestId("tab-labels");
15+
const tabContents = await tabsContainer.getByTestId("tab-contents");
16+
17+
expect(await tabsContainer.count()).toBeTruthy();
18+
expect(await tabLabels.count()).toBeTruthy();
19+
expect(await tabContents.count()).toBeTruthy();
20+
});
21+
22+
test("tabs shift content when clicked", async ({ page }) => {
23+
const section = await page.getByTestId("tabs-test__basic");
24+
const tabsContainer = await section.getByTestId("tabs-container");
25+
const tabLabels = await tabsContainer.getByTestId("tab-labels");
26+
const tabContents = await tabsContainer.getByTestId("tab-contents");
27+
28+
const tabsList = await tabLabels.locator("li").all();
29+
for (const tab of tabsList) {
30+
const label = tab.getByTestId("tab-label");
31+
const labelId = await label.getAttribute("id");
32+
const title = labelId.substring(0, labelId.lastIndexOf("tab") - 1);
33+
const labelIndex = parseInt(
34+
labelId.substring(labelId.lastIndexOf("-") + 1),
35+
);
36+
37+
await label.click();
38+
const content = tabContents.locator(`#${title}-panel-${labelIndex}`);
39+
await expect(content).toBeVisible();
40+
}
41+
});
42+
});

0 commit comments

Comments
 (0)