|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { test, expect } from "@playwright/test"; |
| 4 | +import { setupDebugLog, setupProxy, resolveBinding } from "../support"; |
| 5 | + |
| 6 | +if (!process.env.RUBY_NPM_PACKAGE_ROOT) { |
| 7 | + test.skip("skip", () => {}); |
| 8 | +} else { |
| 9 | + test.beforeEach(async ({ context }) => { |
| 10 | + setupDebugLog(context); |
| 11 | + setupProxy(context, (route, relativePath, mockedPath) => { |
| 12 | + if (relativePath.match("fixtures")) { |
| 13 | + route.fulfill({ |
| 14 | + path: path.join("./test-e2e/integrations", relativePath), |
| 15 | + }); |
| 16 | + } else if (fs.existsSync(mockedPath)) { |
| 17 | + route.fulfill({ |
| 18 | + path: mockedPath, |
| 19 | + }); |
| 20 | + } else { |
| 21 | + route.fulfill({ |
| 22 | + status: 404, |
| 23 | + }); |
| 24 | + } |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + test.describe("JS::RequireRemote#load", () => { |
| 29 | + test("JS::RequireRemote#load returns true", async ({ page }) => { |
| 30 | + const resolve = await resolveBinding(page, "checkResolved"); |
| 31 | + await page.goto( |
| 32 | + "https://cdn.jsdelivr.net/npm/@ruby/head-wasm-wasi@latest/dist/", |
| 33 | + ); |
| 34 | + await page.setContent(` |
| 35 | + <script src="browser.script.iife.js"></script> |
| 36 | + <script type="text/ruby" data-eval="async"> |
| 37 | + require 'js/require_remote' |
| 38 | + JS.global.checkResolved JS::RequireRemote.instance.load 'fixtures/error_on_load_twice' |
| 39 | + </script> |
| 40 | + `); |
| 41 | + |
| 42 | + expect(await resolve()).toBe(true); |
| 43 | + }); |
| 44 | + |
| 45 | + test("JS::RequireRemote#load returns false when same gem is loaded twice", async ({ |
| 46 | + page, |
| 47 | + }) => { |
| 48 | + const resolve = await resolveBinding(page, "checkResolved"); |
| 49 | + await page.goto( |
| 50 | + "https://cdn.jsdelivr.net/npm/@ruby/head-wasm-wasi@latest/dist/", |
| 51 | + ); |
| 52 | + await page.setContent(` |
| 53 | + <script src="browser.script.iife.js"></script> |
| 54 | + <script type="text/ruby" data-eval="async"> |
| 55 | + require 'js/require_remote' |
| 56 | + JS::RequireRemote.instance.load 'fixtures/error_on_load_twice' |
| 57 | + JS.global.checkResolved JS::RequireRemote.instance.load 'fixtures/error_on_load_twice' |
| 58 | + </script> |
| 59 | + `); |
| 60 | + |
| 61 | + expect(await resolve()).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + test("JS::RequireRemote#load throws error when gem is not found", async ({ |
| 65 | + page, |
| 66 | + }) => { |
| 67 | + // Opens the URL that will be used as the basis for determining the relative URL. |
| 68 | + await page.goto( |
| 69 | + "https://cdn.jsdelivr.net/npm/@ruby/head-wasm-wasi@latest/dist/", |
| 70 | + ); |
| 71 | + await page.setContent(` |
| 72 | + <script src="browser.script.iife.js"> |
| 73 | + </script> |
| 74 | + <script type="text/ruby" data-eval="async"> |
| 75 | + require 'js/require_remote' |
| 76 | + JS::RequireRemote.instance.load 'foo' |
| 77 | + </script> |
| 78 | + `); |
| 79 | + |
| 80 | + const error = await page.waitForEvent("pageerror"); |
| 81 | + expect(error.message).toMatch(/cannot load such url -- .+\/foo.rb/); |
| 82 | + }); |
| 83 | + }); |
| 84 | +} |
0 commit comments