From 688b9757b6f632cc499ad17f0254157b78535957 Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Tue, 14 May 2024 04:29:02 +0900 Subject: [PATCH] feat: Save selected language into cookie Refs: #1 --- .../mini18n/snippets/select-lang.html | 8 +++++-- tests/test-e2e/conf.py | 3 +++ tests/test_javascript.py | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/atsphinx/mini18n/templates/mini18n/snippets/select-lang.html b/src/atsphinx/mini18n/templates/mini18n/snippets/select-lang.html index 393be43..0a4b16c 100644 --- a/src/atsphinx/mini18n/templates/mini18n/snippets/select-lang.html +++ b/src/atsphinx/mini18n/templates/mini18n/snippets/select-lang.html @@ -6,9 +6,13 @@
- {%- for code in mini18n.support_languages %} - {%- endfor %} diff --git a/tests/test-e2e/conf.py b/tests/test-e2e/conf.py index 317f894..699235d 100644 --- a/tests/test-e2e/conf.py +++ b/tests/test-e2e/conf.py @@ -1,8 +1,11 @@ # noqa: D100 +from atsphinx.mini18n import get_template_dir extensions = [ "atsphinx.mini18n", ] +html_sidebars = {"**": [f"{get_template_dir()}/mini18n/snippets/select-lang.html"]} + mini18n_default_language = "en" mini18n_support_languages = ["en", "ja"] diff --git a/tests/test_javascript.py b/tests/test_javascript.py index 732d7cc..7d231f1 100644 --- a/tests/test_javascript.py +++ b/tests/test_javascript.py @@ -57,3 +57,24 @@ def test__root(app: SphinxTestApp, page: Page): page.goto(url) time.sleep(0.5) assert page.url.endswith("/en/") + + +@pytest.mark.sphinx("mini18n-html", testroot="e2e") +def test__select_language(app: SphinxTestApp, page: Page): + app.build() + with ServerOnPytest(app.outdir) as server: + url = f"http://localhost:{server.port}/" + page.goto(url) + time.sleep(0.5) + assert page.url.endswith("/en/") + assert not page.context.cookies() + element = page.get_by_label("Language:") + element.select_option(label="ja") + element.evaluate( + "elm => elm.dispatchEvent(new Event('change', {bubbles: true}))" + ) + time.sleep(0.5) + assert page.url.endswith("/ja/index.html") + cookies = page.context.cookies() + assert cookies[0]["name"] == "lang" + assert cookies[0]["value"] == "ja"