app-entrypoint.test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { loadFixture } from './test-utils.js';
  2. import * as assert from 'node:assert/strict';
  3. import { describe, it, before, after } from 'node:test';
  4. import { load as cheerioLoad } from 'cheerio';
  5. import { parseHTML } from 'linkedom';
  6. describe('App Entrypoint', () => {
  7. /** @type {import('./test-utils').Fixture} */
  8. let fixture;
  9. before(async () => {
  10. fixture = await loadFixture({
  11. root: './fixtures/app-entrypoint/',
  12. });
  13. await fixture.build();
  14. });
  15. it('loads during SSR', async () => {
  16. const html = await fixture.readFile('/index.html');
  17. const $ = cheerioLoad(html);
  18. // test 1: basic component renders
  19. assert.equal($('#foo > #bar').text(), 'works');
  20. // test 2: component with multiple script blocks renders and exports
  21. // values from non setup block correctly
  22. assert.equal($('#multiple-script-blocks').text(), '2 4');
  23. // test 3: component using generics renders
  24. assert.equal($('#generics').text(), 'generic');
  25. // test 4: component using generics and multiple script blocks renders
  26. assert.equal($('#generics-and-blocks').text(), '1 3!!!');
  27. });
  28. it('setup included in renderer bundle', async () => {
  29. const data = await fixture.readFile('/index.html');
  30. const { document } = parseHTML(data);
  31. const island = document.querySelector('astro-island');
  32. const client = island.getAttribute('renderer-url');
  33. assert.notEqual(client, undefined);
  34. const js = await fixture.readFile(client);
  35. assert.match(js, /\w+\.component\("Bar"/g);
  36. });
  37. it('loads svg components without transforming them to assets', async () => {
  38. const data = await fixture.readFile('/index.html');
  39. const { document } = parseHTML(data);
  40. const client = document.querySelector('astro-island svg');
  41. assert.notEqual(client, undefined);
  42. });
  43. });
  44. describe('App Entrypoint no export default (dev)', () => {
  45. /** @type {import('./test-utils').Fixture} */
  46. let fixture;
  47. let devServer;
  48. before(async () => {
  49. fixture = await loadFixture({
  50. root: './fixtures/app-entrypoint-no-export-default/',
  51. });
  52. devServer = await fixture.startDevServer();
  53. });
  54. after(async () => {
  55. await devServer.stop();
  56. });
  57. it('loads during SSR', async () => {
  58. const html = await fixture.fetch('/').then((res) => res.text());
  59. const { document } = parseHTML(html);
  60. const bar = document.querySelector('#foo > #bar');
  61. assert.notEqual(bar, undefined);
  62. assert.equal(bar.textContent, 'works');
  63. });
  64. it('loads svg components without transforming them to assets', async () => {
  65. const html = await fixture.fetch('/').then((res) => res.text());
  66. const { document } = parseHTML(html);
  67. const client = document.querySelector('astro-island svg');
  68. assert.notEqual(client, undefined);
  69. });
  70. });
  71. describe('App Entrypoint no export default', () => {
  72. /** @type {import('./test-utils').Fixture} */
  73. let fixture;
  74. before(async () => {
  75. fixture = await loadFixture({
  76. root: './fixtures/app-entrypoint-no-export-default/',
  77. });
  78. await fixture.build();
  79. });
  80. it('loads during SSR', async () => {
  81. const data = await fixture.readFile('/index.html');
  82. const { document } = parseHTML(data);
  83. const bar = document.querySelector('#foo > #bar');
  84. assert.notEqual(bar, undefined);
  85. assert.equal(bar.textContent, 'works');
  86. });
  87. it('component not included in renderer bundle', async () => {
  88. const data = await fixture.readFile('/index.html');
  89. const { document } = parseHTML(data);
  90. const island = document.querySelector('astro-island');
  91. const client = island.getAttribute('renderer-url');
  92. assert.notEqual(client, undefined);
  93. const js = await fixture.readFile(client);
  94. assert.doesNotMatch(js, /\w+\.component\("Bar"/g);
  95. });
  96. it('loads svg components without transforming them to assets', async () => {
  97. const data = await fixture.readFile('/index.html');
  98. const { document } = parseHTML(data);
  99. const client = document.querySelector('astro-island svg');
  100. assert.notEqual(client, undefined);
  101. });
  102. });
  103. describe('App Entrypoint relative', () => {
  104. /** @type {import('./test-utils').Fixture} */
  105. let fixture;
  106. before(async () => {
  107. fixture = await loadFixture({
  108. root: './fixtures/app-entrypoint-relative/',
  109. });
  110. await fixture.build();
  111. });
  112. it('loads during SSR', async () => {
  113. const data = await fixture.readFile('/index.html');
  114. const { document } = parseHTML(data);
  115. const bar = document.querySelector('#foo > #bar');
  116. assert.notEqual(bar, undefined);
  117. assert.equal(bar.textContent, 'works');
  118. });
  119. it('component not included in renderer bundle', async () => {
  120. const data = await fixture.readFile('/index.html');
  121. const { document } = parseHTML(data);
  122. const island = document.querySelector('astro-island');
  123. const client = island.getAttribute('renderer-url');
  124. assert.notEqual(client, undefined);
  125. const js = await fixture.readFile(client);
  126. assert.doesNotMatch(js, /\w+\.component\("Bar"/g);
  127. });
  128. });
  129. describe('App Entrypoint /src/absolute', () => {
  130. /** @type {import('./test-utils').Fixture} */
  131. let fixture;
  132. before(async () => {
  133. fixture = await loadFixture({
  134. root: './fixtures/app-entrypoint-src-absolute/',
  135. });
  136. await fixture.build();
  137. });
  138. it('loads during SSR', async () => {
  139. const data = await fixture.readFile('/index.html');
  140. const { document } = parseHTML(data);
  141. const bar = document.querySelector('#foo > #bar');
  142. assert.notEqual(bar, undefined);
  143. assert.equal(bar.textContent, 'works');
  144. });
  145. it('component not included in renderer bundle', async () => {
  146. const data = await fixture.readFile('/index.html');
  147. const { document } = parseHTML(data);
  148. const island = document.querySelector('astro-island');
  149. const client = island.getAttribute('renderer-url');
  150. assert.notEqual(client, undefined);
  151. const js = await fixture.readFile(client);
  152. assert.doesNotMatch(js, /\w+\.component\("Bar"/g);
  153. });
  154. });
  155. describe('App Entrypoint async', () => {
  156. /** @type {import('./test-utils').Fixture} */
  157. let fixture;
  158. before(async () => {
  159. fixture = await loadFixture({
  160. root: './fixtures/app-entrypoint-async/',
  161. });
  162. await fixture.build();
  163. });
  164. it('loads during SSR', async () => {
  165. const html = await fixture.readFile('/index.html');
  166. const $ = cheerioLoad(html);
  167. // test 1: component before await renders
  168. assert.equal($('#foo > #bar').text(), 'works');
  169. // test 2: component after await renders
  170. assert.equal($('#foo > #baz').text(), 'works');
  171. });
  172. });