Skip to content

Commit

Permalink
Merge branch 'main' into SUMO-5293-add-ts-morph
Browse files Browse the repository at this point in the history
  • Loading branch information
asanehisa authored Oct 19, 2023
2 parents bf1ef2a + dd0820b commit 681d852
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build_and_test_pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ jobs:
call_playwright:
needs: call_build
uses: ./.github/workflows/playwright.yml

call_jstest:
needs: call_build
uses: ./.github/workflows/jstest.yml
secrets:
JSTEST_PRIVATE_SSH_KEY: ${{ secrets.JSTEST_PRIVATE_SSH_KEY }}
42 changes: 42 additions & 0 deletions .github/workflows/jstest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
on:
workflow_call:
secrets:
JSTEST_PRIVATE_SSH_KEY:
description: 'Private key to access the jstest repo'
required: true

jobs:
jstest:
runs-on: ubuntu-latest
defaults:
run:
working-directory: "./packages/pages"
steps:
- name: "Checkout"
uses: actions/checkout@v4

- name: Install pnpm
uses: pnpm/[email protected]

- name: Build with NodeJS 18
uses: actions/setup-node@v3
with:
node-version: 18
cache: "pnpm"

- name: Install deps
run: pnpm i

- name: Pack
run: pnpm pack --pack-destination latest

- name: Checkout JSTEST
uses: actions/checkout@v4
with:
ref: "main"
repository: yext/jstest
ssh-key: ${{ secrets.JSTEST_PRIVATE_SSH_KEY }}
path: jstest

- name: Install and test Pages
run: $GITHUB_WORKSPACE/scripts/install_and_test.sh $GITHUB_WORKSPACE
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ node_modules/
.npm/
.eslintcache
storybook-static
jstest
packages/pages/latest

# Logs
logs
Expand Down
3 changes: 2 additions & 1 deletion packages/pages/src/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ const handler = async ({
if (!noInit) {
await autoYextInit(scope);
}
if (!noGenFeatures)
if (!noGenFeatures) {
await runSubProcess(
"pages generate features",
scope ? ["--scope" + " " + scope] : []
);
}

const devServerPort =
port ??
Expand Down
127 changes: 126 additions & 1 deletion packages/pages/src/util/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,81 @@ describe("isProduction", () => {
name: "browser",
}));

const windowSpy = jest.spyOn(window, "window", "get") as jest.MockInstance<
any,
any
>;
windowSpy.mockImplementation(() => ({
location: {
hostname: "staging.com",
},
}));

expect(isProduction("prod.com")).toBeFalsy();

windowSpy.mockRestore();
runtimeSpy.mockRestore();
});

it("returns false when browser and staging domain and multiple allowed prod domains", async () => {
const runtimeSpy = jest.spyOn(runTime, "getRuntime") as jest.MockInstance<
any,
any
>;
runtimeSpy.mockImplementation(() => ({
name: "browser",
}));

const windowSpy = jest.spyOn(window, "window", "get") as jest.MockInstance<
any,
any
>;
windowSpy.mockImplementation(() => ({
location: {
hostname: "staging.com",
},
}));

expect(isProduction("prod1.com", "prod2.com")).toBeFalsy();

windowSpy.mockRestore();
runtimeSpy.mockRestore();
});

it("returns true when browser and prod domain and multiple allowed prod domains", async () => {
const runtimeSpy = jest.spyOn(runTime, "getRuntime") as jest.MockInstance<
any,
any
>;
runtimeSpy.mockImplementation(() => ({
name: "browser",
}));

const windowSpy = jest.spyOn(window, "window", "get") as jest.MockInstance<
any,
any
>;
windowSpy.mockImplementation(() => ({
location: {
hostname: "prod1.com",
},
}));

expect(isProduction("prod1.com", "prod2.com")).toBeTruthy();

windowSpy.mockRestore();
runtimeSpy.mockRestore();
});

it("returns true when browser and prod domain and no prod domains specified", async () => {
const runtimeSpy = jest.spyOn(runTime, "getRuntime") as jest.MockInstance<
any,
any
>;
runtimeSpy.mockImplementation(() => ({
name: "browser",
}));

const windowSpy = jest.spyOn(window, "window", "get") as jest.MockInstance<
any,
any
Expand All @@ -78,7 +153,57 @@ describe("isProduction", () => {
},
}));

expect(isProduction("staging.com")).toBeFalsy();
expect(isProduction()).toBeTruthy();

windowSpy.mockRestore();
runtimeSpy.mockRestore();
});

it("returns false when browser and localhost and no prod domains specified", async () => {
const runtimeSpy = jest.spyOn(runTime, "getRuntime") as jest.MockInstance<
any,
any
>;
runtimeSpy.mockImplementation(() => ({
name: "browser",
}));

const windowSpy = jest.spyOn(window, "window", "get") as jest.MockInstance<
any,
any
>;
windowSpy.mockImplementation(() => ({
location: {
hostname: "localhost",
},
}));

expect(isProduction()).toBeFalsy();

windowSpy.mockRestore();
runtimeSpy.mockRestore();
});

it("returns false when browser and preview domain and no prod domains specified", async () => {
const runtimeSpy = jest.spyOn(runTime, "getRuntime") as jest.MockInstance<
any,
any
>;
runtimeSpy.mockImplementation(() => ({
name: "browser",
}));

const windowSpy = jest.spyOn(window, "window", "get") as jest.MockInstance<
any,
any
>;
windowSpy.mockImplementation(() => ({
location: {
hostname: "test.preview.pagescdn.com",
},
}));

expect(isProduction()).toBeFalsy();

windowSpy.mockRestore();
runtimeSpy.mockRestore();
Expand Down
25 changes: 19 additions & 6 deletions packages/pages/src/util/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ import { getRuntime } from "./runtime.js";
* Determines if the code is being executed on the production site on
* the client. This is useful for things like firing analytics only
* in production (opposed to dev or staging) and not during server side
* rendering.
* rendering. If one or more domains are provided, the current hostname
* must match one of them in order to be considered a production domain.
* If no domains are provided, all hostnames are considered production
* domains except for localhost and preview.pagescdn.com.
*
* @param domain The production domain of the site
* @param domains The specified production domains of the site
*
* @public
*/
export const isProduction = (domain: string): boolean => {
export const isProduction = (...domains: string[]): boolean => {
const runtime = getRuntime();
if (runtime.name !== "browser") {
return false;
}

return (
runtime.name === "browser" && domain?.includes(window?.location?.hostname)
);
const currentHostname: string = window?.location?.hostname;

if (domains.length === 0) {
return (
currentHostname !== "localhost" &&
!currentHostname.includes("preview.pagescdn.com")
);
}

return domains.some((domain) => domain?.includes(currentHostname));
};
12 changes: 12 additions & 0 deletions scripts/install_and_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
cd $1/jstest/sdks

for D in ./*; do
if [ -d "$D" ]; then
cd "$D"
npm i && npx playwright install \
&& npm install @yext/pages $1/packages/pages/latest/* \
&& npm run test
cd ..
fi
done

0 comments on commit 681d852

Please sign in to comment.