From e032f6f9937b8ccac0f41b8b0bc6bd083ccad188 Mon Sep 17 00:00:00 2001 From: Luke Vella Date: Fri, 15 Apr 2022 18:57:13 +0100 Subject: [PATCH] Keep demo values fixed --- api-client/create-demo.ts | 2 +- .../month-calendar/month-calendar.tsx | 1 + components/switch.tsx | 2 + pages/api/poll/{create-demo.ts => demo.ts} | 102 +++++++++++------- tests/edit-options.spec.ts | 18 ++++ 5 files changed, 83 insertions(+), 42 deletions(-) rename pages/api/poll/{create-demo.ts => demo.ts} (55%) create mode 100644 tests/edit-options.spec.ts diff --git a/api-client/create-demo.ts b/api-client/create-demo.ts index 2a4344e6e..785d68d01 100644 --- a/api-client/create-demo.ts +++ b/api-client/create-demo.ts @@ -2,6 +2,6 @@ import { Poll } from "@prisma/client"; import axios from "axios"; export const createDemo = async (): Promise => { - const { data } = await axios.post("/api/poll/create-demo"); + const { data } = await axios.post("/api/poll/demo"); return data; }; diff --git a/components/forms/poll-options-form/month-calendar/month-calendar.tsx b/components/forms/poll-options-form/month-calendar/month-calendar.tsx index 091b64226..8ab73ef0d 100644 --- a/components/forms/poll-options-form/month-calendar/month-calendar.tsx +++ b/components/forms/poll-options-form/month-calendar/month-calendar.tsx @@ -188,6 +188,7 @@ const MonthCalendar: React.VoidFunctionComponent = ({
{ if (checked) { diff --git a/components/switch.tsx b/components/switch.tsx index ccfdf2331..ab0a6f961 100644 --- a/components/switch.tsx +++ b/components/switch.tsx @@ -12,6 +12,7 @@ const Switch: React.VoidFunctionComponent = ({ checked = false, onChange, srDescription, + ...rest }) => { return ( = ({ "bg-green-500": checked, }, )} + {...rest} > {srDescription ? {srDescription} : null} = []; + + for (let i = 0; i < optionValues.length; i++) { + options.push({ id: await nanoid(), value: optionValues[i] }); + } + + let participants: Array<{ + name: string; + id: string; + createdAt: Date; + }> = []; + + let votes: Array<{ optionId: string; participantId: string }> = []; + + for (let i = 0; i < participantData.length; i++) { + const { name, votes: participantVotes } = participantData[i]; + const participantId = await nanoid(); + participants.push({ + id: participantId, + name, + createdAt: addMinutes(today, i * -1), + }); + + participantVotes.forEach((voteIndex) => { + const option = options[voteIndex]; + votes.push({ optionId: option.id, participantId }); + }); + } + + await prisma.poll.create({ data: { urlId: await nanoid(), verificationCode: await nanoid(), @@ -22,7 +72,7 @@ export default async function handler( location: "Starbucks, 901 New York Avenue", description: "This poll has been automatically generated just for you! Feel free to try out all the different features and when you're ready, you can go to https://rallly.co/new to make a new poll.", - authorName: "John Example", + authorName: "Johnny", verified: true, demo: true, user: { @@ -35,9 +85,7 @@ export default async function handler( }, options: { createMany: { - data: [...Array(4)].map((_, i) => { - return { value: format(addDays(today, i + 1), "yyyy-MM-dd") }; - }), + data: options, }, }, links: { @@ -54,45 +102,17 @@ export default async function handler( ], }, }, - participants: { createMany: { - data: [ - { name: "John", createdAt: addMinutes(today, -1) }, - { name: "Alex", createdAt: addMinutes(today, -2) }, - { name: "Mark", createdAt: addMinutes(today, -3) }, - { name: "Samantha", createdAt: addMinutes(today, -4) }, - ], + data: participants, + }, + }, + votes: { + createMany: { + data: votes, }, }, }, - include: { - options: true, - participants: true, - }, - }); - - const voteData: Array<{ - optionId: string; - participantId: string; - pollId: string; - }> = []; - - // Randomly generate votes - poll.options.forEach((option) => { - poll.participants.forEach((participant) => { - if (Math.random() >= 0.5) { - voteData.push({ - optionId: option.id, - participantId: participant.id, - pollId: poll.urlId, - }); - } - }); - }); - - await prisma.vote.createMany({ - data: voteData, }); return res.json({ urlId: adminUrlId }); diff --git a/tests/edit-options.spec.ts b/tests/edit-options.spec.ts new file mode 100644 index 000000000..2a32c6f96 --- /dev/null +++ b/tests/edit-options.spec.ts @@ -0,0 +1,18 @@ +import { expect, test } from "@playwright/test"; + +test("should show warning when deleting options with votes in them", async ({ + page, +}) => { + await page.goto("/demo"); + + await expect(page.locator('text="Lunch Meeting Demo"')).toBeVisible(); + + await page.click("text='Manage'"); + await page.click("text='Edit options'"); + await page.click("[data-testid='specify-times-switch']"); + await page.click("text='12:00 PM'"); + await page.click("text='1:00 PM'"); + await page.click("text='Save'"); + await expect(page.locator('text="Are you sure?"')).toBeVisible(); + await page.click("text='Delete'"); +});