Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix link generator to handle default branch #5

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
run: npm run test:coverage

- name: 🚀 Release
if: "contains(github.event.head_commit.message, 'release')" # do not release every time during 0.x
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
],
"repository": {
"type": "git",
"url": "https://github.com/utarwyn/storybook-branch-switcher"
"url": "git+https://github.com/utarwyn/storybook-branch-switcher.git"
},
"license": "MIT",
"author": "utarwyn <[email protected]>",
"bin": {
"sb-branch-switcher": "./dist/cli.mjs"
"sb-branch-switcher": "dist/cli.mjs"
},
"exports": {
".": {
Expand Down
49 changes: 49 additions & 0 deletions src/util/location.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, expect, test } from "vitest";
import { generateLink } from "./location";

describe("location", () => {
describe("Method: generateLink", () => {
const location = {
protocol: "https:",
hostname: "company.design",
port: "443",
search: "",
hash: "",
} as Location;

test("should use current host by default", () => {
expect(generateLink(location, null, null, null, null)).toBe(
"https://company.design:443/",
);
});

test("should use targetHost if provided", () => {
expect(generateLink(location, "localhost:3000", null, null, null)).toBe(
"https://localhost:3000/",
);
});

test("should pass same search and hash params to target", () => {
const loc = { ...location, search: "?a=b", hash: "#c=d" };
expect(generateLink(loc, null, null, null, null)).toBe(
"https://company.design:443/?a=b#c=d",
);
});

test.each`
def | current | target | expectedPath | description
${"master"} | ${"master"} | ${"master"} | ${"/"} | ${"target is the current branch"}
${"master"} | ${"master"} | ${"PR-6"} | ${"/PR-6/"} | ${"target is not the default branch"}
${"master"} | ${"PR-6"} | ${"master"} | ${"/"} | ${"target is the default branch"}
${"master"} | ${"PR-6"} | ${"PR-7"} | ${"/PR-7/"} | ${"target is neither the current branch nor the default one"}
`(
"should replace path if $description",
({ def, current, target, expectedPath }) => {
const loc = { ...location, pathname: `/${current}/` };
expect(generateLink(loc, null, def, current, target)).toBe(
`https://company.design:443${expectedPath}`,
);
},
);
});
});
18 changes: 13 additions & 5 deletions src/util/location.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
export const generateLink = (location: Location, targetHost: string, def: string, current: string, target: string): string => {
export const generateLink = (
location: Location,
targetHost: string,
def: string,
current: string,
target: string,
): string => {
const hostname = targetHost || `${location.hostname}:${location.port}`;
let path: string;

if (current && def !== current) {
path = location.pathname.replace(current, target);
} else if (target && def !== target) {
path = `/${target}/`;
if (target !== def) {
if (current === def) {
path = `/${target}/`;
} else {
path = location.pathname.replace(current, target);
}
} else {
path = "/";
}
Expand Down