Skip to content

Commit

Permalink
Fix: Publich Launchpad (#1776)
Browse files Browse the repository at this point in the history
# Motivation

Non logged in users can access Launchpad.

# Changes

* Do not call loadSnsSwapCommitments if not logged in.

# Tests

* Add test for this case.
  • Loading branch information
lmuntaner authored Jan 28, 2023
1 parent fb05ceb commit a86ec35
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
15 changes: 11 additions & 4 deletions frontend/src/lib/pages/Launchpad.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
import { i18n } from "$lib/stores/i18n";
import { SnsSwapLifecycle } from "@dfinity/sns";
import { snsProjectsCommittedStore } from "$lib/derived/sns/sns-projects.derived";
import { onMount } from "svelte";
import { loadSnsSwapCommitments } from "$lib/services/sns.services";
import { authStore } from "$lib/stores/auth.store";
import { isSignedIn } from "$lib/utils/auth.utils";
onMount(() => {
loadSnsSwapCommitments();
});
const loadSnsSale = async () => {
if (!isSignedIn($authStore.identity)) {
return;
}
await loadSnsSwapCommitments();
};
$: $authStore.identity, (async () => await loadSnsSale())();
let showCommitted = false;
$: showCommitted = ($snsProjectsCommittedStore?.length ?? []) > 0;
Expand Down
90 changes: 60 additions & 30 deletions frontend/src/tests/lib/pages/Launchpad.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import { snsProjectsCommittedStore } from "$lib/derived/sns/sns-projects.derived";
import Launchpad from "$lib/pages/Launchpad.svelte";
import { loadSnsSwapCommitments } from "$lib/services/sns.services";
import { authStore } from "$lib/stores/auth.store";
import { render } from "@testing-library/svelte";
import { render, waitFor } from "@testing-library/svelte";
import {
authStoreMock,
mockIdentity,
Expand All @@ -30,42 +31,71 @@ jest.mock("$lib/services/sns.services", () => {
});

describe("Launchpad", () => {
jest
.spyOn(authStore, "subscribe")
.mockImplementation(mutableMockAuthStoreSubscribe);
describe("signed in", () => {
jest
.spyOn(authStore, "subscribe")
.mockImplementation(mutableMockAuthStoreSubscribe);

beforeAll(() =>
authStoreMock.next({
identity: mockIdentity,
})
);
beforeAll(() =>
authStoreMock.next({
identity: mockIdentity,
})
);

afterEach(() => jest.clearAllMocks());
afterEach(() => jest.clearAllMocks());

it("should render titles", () => {
jest
.spyOn(snsProjectsCommittedStore, "subscribe")
.mockImplementation(mockProjectSubscribe([mockSnsFullProject]));
const { getByText } = render(Launchpad);

// TBU
expect(getByText(en.sns_launchpad.open_projects)).toBeInTheDocument();
expect(getByText(en.sns_launchpad.committed_projects)).toBeInTheDocument();
expect(getByText(en.sns_launchpad.proposals)).toBeInTheDocument();
it("should render titles", () => {
jest
.spyOn(snsProjectsCommittedStore, "subscribe")
.mockImplementation(mockProjectSubscribe([mockSnsFullProject]));
const { getByText } = render(Launchpad);

// TBU
expect(getByText(en.sns_launchpad.open_projects)).toBeInTheDocument();
expect(
getByText(en.sns_launchpad.committed_projects)
).toBeInTheDocument();
expect(getByText(en.sns_launchpad.proposals)).toBeInTheDocument();
});

it("should call loadSnsSwapCommitments", async () => {
render(Launchpad);

await waitFor(() => expect(loadSnsSwapCommitments).toHaveBeenCalled());
});

it("should not render committed project title if no committed projects", () => {
jest
.spyOn(snsProjectsCommittedStore, "subscribe")
.mockImplementation(mockProjectSubscribe([]));

const { queryByText } = render(Launchpad);

// TBU
expect(queryByText(en.sns_launchpad.open_projects)).toBeInTheDocument();
expect(
queryByText(en.sns_launchpad.committed_projects)
).not.toBeInTheDocument();
expect(queryByText(en.sns_launchpad.proposals)).toBeInTheDocument();
});
});

it("should not render committed project title if no committed projects", () => {
describe("not logged in", () => {
jest
.spyOn(snsProjectsCommittedStore, "subscribe")
.mockImplementation(mockProjectSubscribe([]));
.spyOn(authStore, "subscribe")
.mockImplementation(mutableMockAuthStoreSubscribe);

const { queryByText } = render(Launchpad);
beforeAll(() =>
authStoreMock.next({
identity: undefined,
})
);
it("should not call loadSnsSwapCommitments", async () => {
render(Launchpad);

// TBU
expect(queryByText(en.sns_launchpad.open_projects)).toBeInTheDocument();
expect(
queryByText(en.sns_launchpad.committed_projects)
).not.toBeInTheDocument();
expect(queryByText(en.sns_launchpad.proposals)).toBeInTheDocument();
await waitFor(() =>
expect(loadSnsSwapCommitments).not.toHaveBeenCalled()
);
});
});
});

0 comments on commit a86ec35

Please sign in to comment.