import { html } from "lit"; import "../../src/components/language-selector.js"; describe("", () => { // Basic Component Tests describe("Component Mounting", () => { it("should mount successfully", () => { cy.mount(html``); cy.get("language-selector").should("exist"); }); it("should be visible in the DOM", () => { cy.mount(html``); cy.get("language-selector").should("be.visible"); }); it("should have proper component structure", () => { cy.mount(html``); cy.get("language-selector") .shadow() .find(".language-selector") .should("exist"); cy.get("language-selector") .shadow() .find("select") .should("exist"); }); }); // Language Options Tests describe("Language Options", () => { beforeEach(() => { cy.mount(html``); }); it("should display all supported languages", () => { const expectedLanguages = [ { value: "en", text: "🇺🇸 English" }, { value: "fr", text: "🇫🇷 Français" }, { value: "es", text: "🇪🇸 Español" } ]; expectedLanguages.forEach(lang => { cy.get("language-selector") .shadow() .find(`option[value="${lang.value}"]`) .should("exist") .should("contain.text", lang.text); }); }); it("should have English selected by default", () => { cy.get("language-selector") .shadow() .find("select") .should("have.value", "en"); }); it("should display flag emojis correctly", () => { cy.get("language-selector") .shadow() .find("option[value='en']") .should("contain.text", "🇺🇸"); cy.get("language-selector") .shadow() .find("option[value='fr']") .should("contain.text", "🇫🇷"); cy.get("language-selector") .shadow() .find("option[value='es']") .should("contain.text", "🇪🇸"); }); }); // Language Selection Tests describe("Language Selection", () => { beforeEach(() => { // Clear localStorage to reset state cy.window().then((win) => { win.localStorage.clear(); }); cy.mount(html``); // Wait for component to be fully mounted and ready cy.get("language-selector") .shadow() .find("select") .should("exist"); // Ensure we start with English - use force to bypass any disabled state during init cy.get("language-selector") .shadow() .find("select") .select("en", { force: true }); // Wait for the reset to complete cy.wait(150); }); it("should allow changing language to French", () => { cy.get("language-selector") .shadow() .find("select") .select("fr") .should("have.value", "fr"); }); it("should allow changing language to Spanish", () => { cy.get("language-selector") .shadow() .find("select") .select("es") .should("have.value", "es"); }); it("should update currentLocale property when selection changes", () => { cy.get("language-selector").then($el => { const component = $el[0] as any; expect(component.currentLocale).to.equal("en"); }); cy.get("language-selector") .shadow() .find("select") .select("fr"); // Wait for async setLocale to complete cy.wait(100); cy.get("language-selector").then($el => { const component = $el[0] as any; expect(component.currentLocale).to.equal("fr"); }); }); it("should dispatch locale-changed event when language changes", () => { let eventReceived = false; let eventDetail: any = null; cy.get("language-selector").then($el => { $el[0].addEventListener('locale-changed', (e: any) => { eventReceived = true; eventDetail = e.detail; }); }); cy.get("language-selector") .shadow() .find("select") .select("es"); // Wait for async setLocale to complete and verify cy.wait(150); cy.then(() => { expect(eventReceived, 'Event should have been received').to.be.true; expect(eventDetail, 'Event detail should exist').to.not.be.null; expect(eventDetail.locale, 'Event locale should be es').to.equal("es"); }); }); it("should not reload the page when language changes", () => { // Component already mounted in beforeEach // Just verify the component still exists after language change // (if it reloaded, the component would remount) cy.get("language-selector") .shadow() .find("select") .select("fr"); // Component should still be there without remounting cy.get("language-selector") .shadow() .find("select") .should("have.value", "fr"); }); }); // LocalStorage Persistence Tests describe("LocalStorage Persistence", () => { beforeEach(() => { // Clear localStorage before each test cy.window().then((win) => { win.localStorage.clear(); }); }); it("should load saved language from localStorage on init", () => { // Set up localStorage before mounting cy.window().then((win) => { win.localStorage.setItem('preferred-locale', 'es'); }); cy.mount(html``); // Give the component time to initialize cy.wait(100); cy.get("language-selector") .shadow() .find("select") .should("have.value", "es"); }); it("should use default language when no saved preference exists", () => { // First, explicitly reset global locale to 'en' by mounting and selecting cy.mount(html``); cy.get("#reset") .shadow() .find("select") .select("en"); cy.wait(150); // Unmount and remount clean cy.get("#reset").then($el => $el.remove()); // Now mount the actual test component cy.mount(html``); cy.get("language-selector") .shadow() .find("select") .should("have.value", "en"); }); it("should update localStorage when language changes", () => { cy.mount(html``); // Change from English to French cy.get("language-selector") .shadow() .find("select") .select("fr"); // Wait for async setLocale cy.wait(100); cy.window().then((win) => { expect(win.localStorage.getItem('preferred-locale')).to.equal('fr'); }); // Change from French to Spanish cy.get("language-selector") .shadow() .find("select") .select("es"); // Wait for async setLocale cy.wait(100); cy.window().then((win) => { expect(win.localStorage.getItem('preferred-locale')).to.equal('es'); }); }); }); // Event Handling Tests describe("Event Handling", () => { it("should emit bubbling and composed events", () => { cy.mount(html`
`); let eventCaught = false; cy.get("#parent").then($parent => { $parent[0].addEventListener('locale-changed', (e: any) => { eventCaught = true; expect(e.bubbles).to.be.true; expect(e.composed).to.be.true; }); }); cy.get("language-selector") .shadow() .find("select") .select("fr"); // Wait for async event dispatch cy.wait(100).then(() => { expect(eventCaught).to.be.true; }); }); it("should include correct locale information in event detail", () => { cy.mount(html``); let receivedDetail = null; cy.get("language-selector").then($el => { $el[0].addEventListener('locale-changed', (e: any) => { receivedDetail = e.detail; }); }); cy.get("language-selector") .shadow() .find("select") .select("es"); // Wait for async event dispatch cy.wait(100).then(() => { expect(receivedDetail).to.deep.equal({ locale: "es" }); }); }); it("should handle rapid language changes correctly", () => { cy.mount(html``); const eventsReceived: string[] = []; cy.get("language-selector").then($el => { $el[0].addEventListener('locale-changed', (e: any) => { eventsReceived.push(e.detail.locale); }); }); // Change languages with waits between each to allow async completion cy.get("language-selector") .shadow() .find("select") .select("fr"); cy.wait(50); cy.get("language-selector") .shadow() .find("select") .select("es"); cy.wait(50); cy.get("language-selector") .shadow() .find("select") .select("en"); // Wait for all async operations to complete cy.wait(100).then(() => { expect(eventsReceived).to.deep.equal(["fr", "es", "en"]); }); }); }); // Error Handling Tests describe("Error Handling", () => { beforeEach(() => { cy.mount(html``); }); it("should not crash when changing language", () => { // Should not crash when selecting a language cy.get("language-selector") .shadow() .find("select") .select("fr"); // Wait for async operations cy.wait(100); // Component should still exist and be functional cy.get("language-selector").should("exist"); cy.get("language-selector") .shadow() .find("select") .should("have.value", "fr"); }); it("should maintain component state after language changes", () => { cy.get("language-selector") .shadow() .find("select") .select("es"); cy.wait(100); // Component should still be functional cy.get("language-selector").should("exist"); // Should be able to change again cy.get("language-selector") .shadow() .find("select") .select("en"); cy.wait(100); cy.get("language-selector") .shadow() .find("select") .should("have.value", "en"); }); }); // Styling Tests describe("Styling and Appearance", () => { beforeEach(() => { cy.mount(html``); }); it("should have proper TEMS styling applied", () => { cy.get("language-selector") .shadow() .find("select") .should("have.css", "border-radius", "6px") .should("have.css", "cursor", "pointer"); }); it("should have custom dropdown arrow", () => { cy.get("language-selector") .shadow() .find("select") .should("have.css", "appearance", "none") .should("have.css", "background-repeat", "no-repeat"); }); it("should show focus styles when focused", () => { cy.get("language-selector") .shadow() .find("select") .focus() .should("be.focused") .and("have.css", "outline") .and("match", /none|0px/); // Note: box-shadow is harder to test with exact values due to browser differences }); it("should be visible and properly sized", () => { cy.get("language-selector") .shadow() .find("select") .should("be.visible") .should("have.css", "padding"); }); }); // Accessibility Tests describe("Accessibility", () => { beforeEach(() => { // Clear localStorage cy.window().then((win) => { win.localStorage.clear(); }); cy.mount(html``); // Reset to English cy.get("language-selector") .shadow() .find("select") .select("en"); cy.wait(150); }); it("should be keyboard accessible", () => { cy.get("language-selector") .shadow() .find("select") .focus() .should("be.focused"); }); it("should support arrow key navigation", () => { // Verify keyboard interaction works - select is focusable and can be changed cy.get("language-selector") .shadow() .find("select") .should("have.value", "en") .focus() .should("be.focused"); // Select can be changed (keyboard accessible) cy.get("language-selector") .shadow() .find("select") .select("fr"); cy.wait(150); cy.get("language-selector") .shadow() .find("select") .should("have.value", "fr"); }); it("should support enter key for selection", () => { // Verify keyboard interaction works cy.get("language-selector") .shadow() .find("select") .should("have.value", "en") .focus() .should("be.focused"); // Select can be changed via keyboard (testing accessibility) cy.get("language-selector") .shadow() .find("select") .select("fr"); cy.wait(150); cy.get("language-selector") .shadow() .find("select") .should("have.value", "fr"); }); it("should have semantic HTML structure", () => { cy.get("language-selector") .shadow() .find("select") .should("exist"); cy.get("language-selector") .shadow() .find("option") .should("have.length", 3); }); }); // Integration Tests describe("Integration with Parent Components", () => { it("should work when embedded in other components", () => { cy.mount(html`

Settings Panel

`); cy.get("language-selector").should("exist"); cy.get("language-selector") .shadow() .find("select") .should("be.visible"); }); it("should handle multiple instances independently", () => { cy.mount(html`
`); // Change first selector to French cy.get("#selector1") .shadow() .find("select") .select("fr"); // Change second selector to Spanish cy.get("#selector2") .shadow() .find("select") .select("es"); // Verify they maintain independent state cy.get("#selector1") .shadow() .find("select") .should("have.value", "fr"); cy.get("#selector2") .shadow() .find("select") .should("have.value", "es"); }); it("should emit events that parent components can catch", () => { const events: any[] = []; cy.mount(html`
`); cy.get("#container").then($container => { $container[0].addEventListener('locale-changed', (e: any) => { events.push(e.detail); }); }); cy.get("language-selector") .shadow() .find("select") .select("fr"); // Wait for async event dispatch cy.wait(100).then(() => { expect(events).to.have.length(1); expect(events[0].locale).to.equal("fr"); }); }); }); // Performance Tests describe("Performance", () => { it("should initialize quickly", () => { cy.mount(html``); // Just verify it exists and can be interacted with cy.get("language-selector").should("exist"); cy.get("language-selector") .shadow() .find("select") .should("exist") .and("not.have.value", ""); // Has a non-empty value }); it("should handle rapid selections without issues", () => { cy.mount(html``); // Rapidly change selections for (let i = 0; i < 10; i++) { cy.get("language-selector") .shadow() .find("select") .select(i % 2 === 0 ? "fr" : "es"); } // Should still be functional cy.get("language-selector") .shadow() .find("select") .should("exist"); }); }); });