Skip to content

Commit

Permalink
test: write snapshot tests - workflow test
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcho committed Oct 25, 2024
1 parent cfd4165 commit baa1f24
Show file tree
Hide file tree
Showing 25 changed files with 2,031 additions and 1,756 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ dist-ssr
*.njsproj
*.sln
*.sw?
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
4 changes: 4 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
compressionLevel: mixed

enableGlobalCache: false

nodeLinker: node-modules
77 changes: 77 additions & 0 deletions __visual_tests__/workflow1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {test, expect, Page} from '@playwright/test';

const TEST_URL = 'http://localhost:5173/chat-ai-widget/?app_id=833E2DC4-DFA2-4508-A283-6E5C7BFCF18A&bot_id=onboarding_bot&disable_timestamps=true';

export async function assertScreenshot(page: Page, screenshotName: string) {
await expect(page.locator('#aichatbot-widget-window')).toHaveScreenshot(
`${screenshotName}.png`,
{
omitBackground: false,
// threshold: 0.1, // Keep this in case you need it. It is for letting tests with little difference in pixels to pass.
});
}

test.beforeEach(async ({ page }) => {
await page.goto(TEST_URL);
});

/**
* 100
* Test workflow1
* Scenario:
* 1. Trigger workflow1
* 2. Get form message response
* 3. Submit without filling the form
* 4. Fill form message and then submit
* 5. Go back
* 6. Get text message response
* Verify after each step
*/
test('100', async ({ page, browserName }) => {
await page.click('#aichatbot-widget-button');
await page.waitForTimeout(2500);
const input = page.locator('#sendbird-message-input-text-field');

// 1
await input.fill('trigger workflow1');
await assertScreenshot(page, `100-1.${browserName}`);
await input.press('Enter');
await page.waitForTimeout(500);
await assertScreenshot(page, `100-2.${browserName}`);

// 2
let options = page.locator('.sendbird-suggested-replies__option');
await options.first().click();
await page.waitForTimeout(500);
await assertScreenshot(page, `100-3.${browserName}`);

// 3
let submitButton = page.locator('button.sendbird-button--primary');
await submitButton.click();
await assertScreenshot(page, `100-4.${browserName}`);

// 4
const inputs = page.locator('.sendbird-input__input');
await inputs.nth(0).fill('guy ordering food');
await inputs.nth(2).fill('2');
await inputs.nth(3).fill('[email protected]');
await inputs.nth(4).fill('123-456-7890');
const chipContainer = page.locator('.sendbird-form-chip__container');
await chipContainer.locator(':scope > *').nth(5).click();
submitButton = page.locator('button.sendbird-button--primary');
await submitButton.click();
await page.waitForTimeout(100);
await assertScreenshot(page, `100-5.${browserName}`);

// 5
options = page.locator('.sendbird-suggested-replies__option');
await options.first().click();
await page.waitForTimeout(100);
await assertScreenshot(page, `100-6.${browserName}`);

// 6
options = page.locator('.sendbird-suggested-replies__option');
await options.nth(1).click();
await page.waitForTimeout(100);
await assertScreenshot(page, `100-7.${browserName}`);
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"@linaria/atomic": "^6.2.0",
"@linaria/core": "^6.2.0",
"@linaria/react": "^6.2.1",
"@playwright/test": "^1.48.1",
"@types/dompurify": "^3.0.5",
"@types/node": "^22.7.9",
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
"@types/styled-components": "^5.1.26",
Expand Down
79 changes: 79 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { defineConfig, devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './__visual_tests__',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'yarn dev',
url: 'http://localhost:5173/chat-ai-widget/',
reuseExistingServer: !process.env.CI,
},
});
12 changes: 9 additions & 3 deletions src/components/BotMessageWithBodyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const HEIGHTS = {

export default function BotMessageWithBodyInput(props: Props) {
const { botUser } = useChatContext();
const { botStudioEditProps, dateLocale } = useConstantState();
const { botStudioEditProps, dateLocale, stringSet } = useConstantState();

const { createdAt, bodyComponent, chainTop, chainBottom, messageFeedback, wideContainer = false } = props;

Expand Down Expand Up @@ -86,10 +86,16 @@ export default function BotMessageWithBodyInput(props: Props) {
<Content>
{bodyComponent}
{!wideContainer && !!createdAt && (
<DefaultSentTime>{formatCreatedAtToAMPM(createdAt, dateLocale)}</DefaultSentTime>
<DefaultSentTime>
{formatCreatedAtToAMPM(createdAt, stringSet.MESSAGE_TIMESTAMP_FORMAT, dateLocale)}
</DefaultSentTime>
)}
</Content>
{wideContainer && !!createdAt && <WideSentTime>{formatCreatedAtToAMPM(createdAt, dateLocale)}</WideSentTime>}
{wideContainer && !!createdAt && (
<WideSentTime>
{formatCreatedAtToAMPM(createdAt, stringSet.MESSAGE_TIMESTAMP_FORMAT, dateLocale)}
</WideSentTime>
)}
{displayProfileImage && messageFeedback}
</FullBodyContainer>
</Root>
Expand Down
8 changes: 7 additions & 1 deletion src/components/MyMessageStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Locale } from 'date-fns';
import { useTheme } from 'styled-components';

import { DefaultSentTime } from './MessageComponent';
import { useConstantState } from '../context/ConstantContext';
import { Icon } from '../foundation/components/Icon';
import { Loader } from '../foundation/components/Loader';
import { formatCreatedAtToAMPM } from '../utils/messageTimestamp';
Expand All @@ -16,6 +17,7 @@ interface MyMessageStatusProps {

export default function MyMessageStatus(props: MyMessageStatusProps) {
const { message, dateLocale } = props;
const { stringSet } = useConstantState();
const theme = useTheme();

switch (message.sendingStatus) {
Expand All @@ -32,7 +34,11 @@ export default function MyMessageStatus(props: MyMessageStatusProps) {
</div>
);
default:
return <DefaultSentTime>{formatCreatedAtToAMPM(message.createdAt, dateLocale)}</DefaultSentTime>;
return (
<DefaultSentTime>
{formatCreatedAtToAMPM(message.createdAt, stringSet.MESSAGE_TIMESTAMP_FORMAT, dateLocale)}
</DefaultSentTime>
);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/components/UserMessageWithBodyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import styled from 'styled-components';
import Avatar from '@uikit/ui/Avatar';

import { SentTime } from './MessageComponent';
import { useConstantState } from '../context/ConstantContext';
import { Label } from '../foundation/components/Label';
import { formatCreatedAtToAMPM } from '../utils/messageTimestamp';

Expand Down Expand Up @@ -60,6 +61,7 @@ const EmptyImageContainer = styled.div`

export default function UserMessageWithBodyInput(props: Props) {
const { user, message, bodyComponent, chainTop, chainBottom, locale } = props;
const { stringSet } = useConstantState();

const nonChainedMessage = chainTop == null && chainBottom == null;
const displayProfileImage = nonChainedMessage || chainBottom;
Expand All @@ -82,7 +84,9 @@ export default function UserMessageWithBodyInput(props: Props) {
)}
<Content>
{bodyComponent}
{!!message?.createdAt && <SentTime>{formatCreatedAtToAMPM(message.createdAt, locale)}</SentTime>}
{!!message?.createdAt && (
<SentTime>{formatCreatedAtToAMPM(message.createdAt, stringSet.MESSAGE_TIMESTAMP_FORMAT, locale)}</SentTime>
)}
</Content>
</BodyContainer>
</Root>
Expand Down
25 changes: 23 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const WidgetApp = () => {
const urlParams = new URLSearchParams(window.location.search);
const appId = urlParams.get('app_id') ?? import.meta.env.VITE_CHAT_WIDGET_APP_ID;
const botId = urlParams.get('bot_id') ?? import.meta.env.VITE_CHAT_WIDGET_BOT_ID;
const disableTimestampsStr = urlParams.get('disable_timestamps');
let disableTimestamps = false;
if (disableTimestampsStr !== null) {
disableTimestamps = JSON.parse(disableTimestampsStr);
}
const locale = urlParams.get('locale') ?? undefined;
const region = urlParams.get('region') ?? undefined;

Expand All @@ -23,9 +28,25 @@ const WidgetApp = () => {
if (!appId || !botId) {
return null;
}

const host = getHost(region);
return <App applicationId={appId} botId={botId} locale={locale} apiHost={host.apiHost} wsHost={host.wsHost} />;
return (
<App
applicationId={appId}
botId={botId}
locale={locale}
apiHost={host.apiHost}
wsHost={host.wsHost}
stringSet={
disableTimestamps
? {
DATE_FORMAT__MESSAGE_LIST__DATE_SEPARATOR: "'DATE SEPARATOR'",
MESSAGE_TIMESTAMP_FORMAT: "'SENT TIME'",
}
: undefined
}
/>
);
};

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/messageTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { enUS } from 'date-fns/locale';
* Note that returned time is computed based on the running app's region but not the injected locale value.
* So result varies depending on the location of the running app.
*/
export function formatCreatedAtToAMPM(createdAt: number, locale: Locale = enUS) {
const time = format(createdAt || 0, 'p', { locale });
export function formatCreatedAtToAMPM(createdAt: number, formatString = 'p', locale: Locale = enUS) {
const time = format(createdAt || 0, formatString, { locale });
return time;
}
Loading

0 comments on commit baa1f24

Please sign in to comment.