From 13fccc3b9f4cc876c058124a2136a668aa0ae9e7 Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 18 Feb 2022 18:48:28 +0530 Subject: [PATCH] Added tests for direct portal links closes https://github.com/TryGhost/Team/issues/1354 - extends test coverage to include direct portal links - `#/portal/*` --- .../portal/src/tests/data-attributes.test.js | 13 +- ghost/portal/src/tests/portal-links.test.js | 194 ++++++++++++++++++ 2 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 ghost/portal/src/tests/portal-links.test.js diff --git a/ghost/portal/src/tests/data-attributes.test.js b/ghost/portal/src/tests/data-attributes.test.js index b2ccf8bd8c..8450983da2 100644 --- a/ghost/portal/src/tests/data-attributes.test.js +++ b/ghost/portal/src/tests/data-attributes.test.js @@ -1,7 +1,6 @@ import App from '../App'; import {site as FixturesSite, member as FixtureMember} from '../utils/test-fixtures'; import {fireEvent, appRender, within} from '../utils/test-utils'; -import {site as FixtureSite} from '../utils/test-fixtures'; import setupGhostApi from '../utils/api'; const {formSubmitHandler, planClickHandler} = require('../data-attributes'); @@ -275,7 +274,7 @@ describe('Portal Data attributes:', () => { let { popupFrame, triggerButtonFrame, ...utils } = await setup({ - site: FixtureSite.singleTier.basic, + site: FixturesSite.singleTier.basic, showPopup: false }); expect(popupFrame).not.toBeInTheDocument(); @@ -295,7 +294,7 @@ describe('Portal Data attributes:', () => { let { popupFrame, triggerButtonFrame, ...utils } = await setup({ - site: FixtureSite.singleTier.basic, + site: FixturesSite.singleTier.basic, showPopup: false }); expect(popupFrame).not.toBeInTheDocument(); @@ -317,7 +316,7 @@ describe('Portal Data attributes:', () => { let { popupFrame, triggerButtonFrame, ...utils } = await setup({ - site: FixtureSite.singleTier.basic, + site: FixturesSite.singleTier.basic, showPopup: false }); expect(popupFrame).not.toBeInTheDocument(); @@ -339,7 +338,7 @@ describe('Portal Data attributes:', () => { let { popupFrame, triggerButtonFrame, ...utils } = await setup({ - site: FixtureSite.singleTier.basic, + site: FixturesSite.singleTier.basic, member: FixtureMember.free, showPopup: false }); @@ -362,7 +361,7 @@ describe('Portal Data attributes:', () => { let { popupFrame, triggerButtonFrame, ...utils } = await setup({ - site: FixtureSite.singleTier.basic, + site: FixturesSite.singleTier.basic, member: FixtureMember.free, showPopup: false }); @@ -385,7 +384,7 @@ describe('Portal Data attributes:', () => { let { popupFrame, triggerButtonFrame, ...utils } = await setup({ - site: FixtureSite.singleTier.basic, + site: FixturesSite.singleTier.basic, member: FixtureMember.free, showPopup: false }); diff --git a/ghost/portal/src/tests/portal-links.test.js b/ghost/portal/src/tests/portal-links.test.js new file mode 100644 index 0000000000..dedba92538 --- /dev/null +++ b/ghost/portal/src/tests/portal-links.test.js @@ -0,0 +1,194 @@ +import App from '../App'; +import {site as FixtureSite, member as FixtureMember} from '../utils/test-fixtures'; +import {appRender, within} from '../utils/test-utils'; +import setupGhostApi from '../utils/api'; + +const setup = async ({site, member = null, showPopup = true}) => { + const ghostApi = setupGhostApi({siteUrl: 'https://example.com'}); + ghostApi.init = jest.fn(() => { + return Promise.resolve({ + site, + member + }); + }); + + ghostApi.member.sendMagicLink = jest.fn(() => { + return Promise.resolve('success'); + }); + + ghostApi.member.checkoutPlan = jest.fn(() => { + return Promise.resolve(); + }); + + const utils = appRender( + + ); + + const triggerButtonFrame = await utils.findByTitle(/portal-trigger/i); + const popupFrame = utils.queryByTitle(/portal-popup/i); + return { + ghostApi, + popupFrame, + triggerButtonFrame, + ...utils + }; +}; + +describe('Portal Data links:', () => { + beforeEach(() => { + // Mock global fetch + jest.spyOn(window, 'fetch').mockImplementation((url) => { + if (url.includes('send-magic-link')) { + return Promise.resolve({ + ok: true, + json: async () => ({success: true}) + }); + } + + if (url.includes('api/session')) { + return Promise.resolve({ + ok: true, + text: async () => { + return 'session-identity'; + } + }); + } + + if (url.includes('create-stripe-checkout-session')) { + return Promise.resolve({ + ok: true, + json: async () => { + return { + publicKey: 'key-xyz' + }; + } + }); + } + return Promise.resolve({}); + }); + + // Mock global Stripe + window.Stripe = () => {}; + jest.spyOn(window, 'Stripe').mockImplementation(() => { + return { + redirectToCheckout: () => { + return Promise.resolve({}); + } + }; + }); + + // Mock window.location + let locationMock = jest.fn(); + delete window.location; + window.location = {assign: locationMock}; + window.location.href = (new URL('https://portal.localhost')).href; + }); + afterEach(() => { + jest.restoreAllMocks(); + window.location.hash = ''; + }); + describe('#/portal', () => { + test('opens default portal page', async () => { + window.location.hash = '#/portal'; + let { + popupFrame, triggerButtonFrame, ...utils + } = await setup({ + site: FixtureSite.singleTier.basic, + showPopup: false + }); + expect(triggerButtonFrame).toBeInTheDocument(); + popupFrame = await utils.findByTitle(/portal-popup/i); + expect(popupFrame).toBeInTheDocument(); + const signupTitle = within(popupFrame.contentDocument).queryByText(/already a member/i); + expect(signupTitle).toBeInTheDocument(); + }); + }); + + describe('#/portal/signin', () => { + test('opens portal signin page', async () => { + window.location.hash = '#/portal/signin'; + let { + popupFrame, triggerButtonFrame, ...utils + } = await setup({ + site: FixtureSite.singleTier.basic, + showPopup: false + }); + expect(triggerButtonFrame).toBeInTheDocument(); + popupFrame = await utils.findByTitle(/portal-popup/i); + expect(popupFrame).toBeInTheDocument(); + const loginTitle = within(popupFrame.contentDocument).queryByText(/log in/i); + expect(loginTitle).toBeInTheDocument(); + }); + }); + + describe('#/portal/signup', () => { + test('opens portal signup page', async () => { + window.location.hash = '#/portal/signup'; + let { + popupFrame, triggerButtonFrame, ...utils + } = await setup({ + site: FixtureSite.singleTier.basic, + showPopup: false + }); + expect(triggerButtonFrame).toBeInTheDocument(); + popupFrame = await utils.findByTitle(/portal-popup/i); + expect(popupFrame).toBeInTheDocument(); + const signupTitle = within(popupFrame.contentDocument).queryByText(/already a member/i); + expect(signupTitle).toBeInTheDocument(); + }); + }); + + describe('#/portal/account', () => { + test('opens portal account home page', async () => { + window.location.hash = '#/portal/account'; + let { + popupFrame, triggerButtonFrame, ...utils + } = await setup({ + site: FixtureSite.singleTier.basic, + member: FixtureMember.free, + showPopup: false + }); + expect(triggerButtonFrame).toBeInTheDocument(); + popupFrame = await utils.findByTitle(/portal-popup/i); + expect(popupFrame).toBeInTheDocument(); + const accountHomeTitle = within(popupFrame.contentDocument).queryByText(/your account/i); + expect(accountHomeTitle).toBeInTheDocument(); + }); + }); + + describe('#/portal/account/plans', () => { + test('opens portal account plan page', async () => { + window.location.hash = '#/portal/account/plans'; + let { + popupFrame, triggerButtonFrame, ...utils + } = await setup({ + site: FixtureSite.singleTier.basic, + member: FixtureMember.free, + showPopup: false + }); + expect(triggerButtonFrame).toBeInTheDocument(); + popupFrame = await utils.findByTitle(/portal-popup/i); + expect(popupFrame).toBeInTheDocument(); + const accountPlanTitle = within(popupFrame.contentDocument).queryByText(/choose a plan/i); + expect(accountPlanTitle).toBeInTheDocument(); + }); + }); + + describe('#/portal/account/profile', () => { + test('opens portal account profile page', async () => { + window.location.hash = '#/portal/account/profile'; + let { + popupFrame, triggerButtonFrame, ...utils + } = await setup({ + site: FixtureSite.singleTier.basic, + member: FixtureMember.free, + showPopup: false + }); + expect(triggerButtonFrame).toBeInTheDocument(); + popupFrame = await utils.findByTitle(/portal-popup/i); + expect(popupFrame).toBeInTheDocument(); + const accountProfileTitle = within(popupFrame.contentDocument).queryByText(/account settings/i); + expect(accountProfileTitle).toBeInTheDocument(); + }); + }); +});