|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import createEjsLoader from "./ejs"; |
| 3 | + |
| 4 | +describe("EJS Loader", () => { |
| 5 | + const loader = createEjsLoader().setDefaultLocale("en"); |
| 6 | + |
| 7 | + describe("pull", () => { |
| 8 | + it("should extract translatable text from simple EJS template", async () => { |
| 9 | + const input = ` |
| 10 | + <h1>Welcome to our website</h1> |
| 11 | + <p>Hello <%= name %>, you have <%= messages.length %> messages.</p> |
| 12 | + <footer>© 2024 Our Company</footer> |
| 13 | + `; |
| 14 | + |
| 15 | + const result = await loader.pull("en", input); |
| 16 | + |
| 17 | + // Check that we have extracted some translatable content |
| 18 | + expect(Object.keys(result).length).toBeGreaterThan(0); |
| 19 | + |
| 20 | + // Check that the EJS variables are not included in the translatable text |
| 21 | + const allValues = Object.values(result).join(' '); |
| 22 | + expect(allValues).not.toContain('<%= name %>'); |
| 23 | + expect(allValues).not.toContain('<%= messages.length %>'); |
| 24 | + |
| 25 | + // Check that we have the main content |
| 26 | + expect(allValues).toContain('Welcome to our website'); |
| 27 | + expect(allValues).toContain('Hello'); |
| 28 | + expect(allValues).toContain('messages'); |
| 29 | + expect(allValues).toContain('© 2024 Our Company'); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should handle EJS templates with various tag types", async () => { |
| 33 | + const input = ` |
| 34 | + <div> |
| 35 | + <h2>User Dashboard</h2> |
| 36 | + <% if (user.isAdmin) { %> |
| 37 | + <p>Admin Panel</p> |
| 38 | + <% } %> |
| 39 | + <%# This is a comment %> |
| 40 | + <p>Welcome back, <%- user.name %></p> |
| 41 | + <span>Last login: <%= formatDate(user.lastLogin) %></span> |
| 42 | + </div> |
| 43 | + `; |
| 44 | + |
| 45 | + const result = await loader.pull("en", input); |
| 46 | + |
| 47 | + expect(result).toHaveProperty("text_0"); |
| 48 | + expect(result).toHaveProperty("text_1"); |
| 49 | + expect(Object.keys(result).length).toBeGreaterThan(0); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should handle empty input", async () => { |
| 53 | + const result = await loader.pull("en", ""); |
| 54 | + expect(result).toEqual({}); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should handle input with only EJS tags", async () => { |
| 58 | + const input = "<%= variable %><% if (condition) { %><% } %>"; |
| 59 | + const result = await loader.pull("en", input); |
| 60 | + expect(result).toEqual({}); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should handle mixed content", async () => { |
| 64 | + const input = ` |
| 65 | + Welcome <%= user.name %>! |
| 66 | + <% for (let i = 0; i < items.length; i++) { %> |
| 67 | + Item: <%= items[i].name %> |
| 68 | + <% } %> |
| 69 | + Thank you for visiting. |
| 70 | + `; |
| 71 | + |
| 72 | + const result = await loader.pull("en", input); |
| 73 | + expect(Object.keys(result).length).toBeGreaterThan(0); |
| 74 | + expect(Object.values(result).some(text => text.includes("Welcome"))).toBe(true); |
| 75 | + expect(Object.values(result).some(text => text.includes("Thank you"))).toBe(true); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("push", () => { |
| 80 | + it("should reconstruct EJS template with translated content", async () => { |
| 81 | + const originalInput = `<h1>Welcome</h1><p>Hello <%= name %></p>`; |
| 82 | + |
| 83 | + // First pull to get the structure |
| 84 | + const pulled = await loader.pull("en", originalInput); |
| 85 | + |
| 86 | + // Static translated data object based on actual loader behavior |
| 87 | + const translated = { |
| 88 | + text_0: "Bienvenido", |
| 89 | + text_1: "Hola" |
| 90 | + }; |
| 91 | + |
| 92 | + const result = await loader.push("es", translated); |
| 93 | + |
| 94 | + // Test against the expected reconstructed string |
| 95 | + const expectedOutput = `<h1>Bienvenido</h1><p>Hola <%= name %></p>`; |
| 96 | + |
| 97 | + expect(result).toBe(expectedOutput); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should handle complex EJS templates", async () => { |
| 101 | + const originalInput = `<h2>Dashboard</h2><% if (user) { %><p>Welcome</p><% } %>`; |
| 102 | + |
| 103 | + const pulled = await loader.pull("en", originalInput); |
| 104 | + |
| 105 | + // Static translated data object |
| 106 | + const translated = { |
| 107 | + text_0: "Tablero", |
| 108 | + text_1: "Bienvenido" |
| 109 | + }; |
| 110 | + |
| 111 | + const result = await loader.push("es", translated); |
| 112 | + |
| 113 | + // Test against the expected reconstructed string |
| 114 | + const expectedOutput = `<h2>Tablero</h2><% if (user) { %><p>Bienvenido</p><% } %>`; |
| 115 | + |
| 116 | + expect(result).toBe(expectedOutput); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should handle missing original input", async () => { |
| 120 | + const translated = { |
| 121 | + text_0: "Hello World", |
| 122 | + text_1: "This is a test" |
| 123 | + }; |
| 124 | + |
| 125 | + const result = await loader.push("es", translated); |
| 126 | + |
| 127 | + expect(result).toContain("Hello World"); |
| 128 | + expect(result).toContain("This is a test"); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe("round trip", () => { |
| 133 | + it("should maintain EJS functionality after round trip", async () => { |
| 134 | + const originalInput = ` |
| 135 | + <h1>Welcome <%= title %></h1> |
| 136 | + <% if (showMessage) { %> |
| 137 | + <p>Hello <%= user.name %>, you have <%= count %> new messages.</p> |
| 138 | + <% } %> |
| 139 | + <ul> |
| 140 | + <% items.forEach(function(item) { %> |
| 141 | + <li><%= item.name %> - $<%= item.price %></li> |
| 142 | + <% }); %> |
| 143 | + </ul> |
| 144 | + <footer>Contact us at [email protected]</footer> |
| 145 | + `; |
| 146 | + |
| 147 | + // Pull original content |
| 148 | + const pulled = await loader.pull("en", originalInput); |
| 149 | + |
| 150 | + // Push back without translation (should be identical) |
| 151 | + const reconstructed = await loader.push("en", pulled); |
| 152 | + |
| 153 | + // Verify EJS tags are preserved |
| 154 | + expect(reconstructed).toContain("<%= title %>"); |
| 155 | + expect(reconstructed).toContain("<% if (showMessage) { %>"); |
| 156 | + expect(reconstructed).toContain("<%= user.name %>"); |
| 157 | + expect(reconstructed).toContain("<%= count %>"); |
| 158 | + expect(reconstructed).toContain("<% items.forEach(function(item) { %>"); |
| 159 | + expect(reconstructed).toContain("<%= item.name %>"); |
| 160 | + expect(reconstructed).toContain("<%= item.price %>"); |
| 161 | + expect(reconstructed).toContain("<% }); %>"); |
| 162 | + expect(reconstructed).toContain("Contact us at [email protected]"); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments