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

test(dialog-box): add ui-test #51

Merged
merged 1 commit into from
Sep 23, 2024
Merged

test(dialog-box): add ui-test #51

merged 1 commit into from
Sep 23, 2024

Conversation

James-9696
Copy link
Collaborator

@James-9696 James-9696 commented Sep 23, 2024

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • Tests
    • Introduced a new test suite for the dialog box component, validating functionalities through UI screenshots.
    • Tests cover scenarios including basic usage, layout example, right-side dialog, and centered title, ensuring visual accuracy and error handling.

Copy link

coderabbitai bot commented Sep 23, 2024

Walkthrough

A new test suite has been added for the dialog-box component in the file tests/dialog-box/xdesign.spec.ts. This suite includes four tests that verify the functionality of the dialog box through UI screenshots, ensuring that it behaves as expected in various scenarios. Each test checks for the dialog's visibility in the viewport and captures screenshots for visual validation.

Changes

File Change Summary
tests/dialog-box/xdesign.spec.ts Introduced a new test suite with four tests for the dialog-box component, validating UI functionality through screenshots.

Possibly related PRs

Suggested labels

e2e

Suggested reviewers

  • zzcr

🐰 In the meadow where bunnies play,
A dialog box came out to say,
"With tests so bright,
I shine with delight,
For every click, I’m here to stay!"
🌼✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Tip

Early access (new models): enabled

We are currently testing new code review model(s) that may lead to higher noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

@github-actions github-actions bot added the e2e playwright label Sep 23, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Outside diff range and nitpick comments (1)
tests/dialog-box/xdesign.spec.ts (1)

4-12: LGTM! Consider adding a more specific selector for the dialog button.

The test case for basic usage is well-structured and covers the essential aspects of the dialog-box component. Good job on including error handling and visual verification.

Consider using a more specific selector for the dialog button. Instead of:

await demo.getByRole('button', { name: /Dialog/ }).click()

You could use:

await demo.getByRole('button', { name: 'Open Dialog', exact: true }).click()

This would make the test more robust against potential changes in button text or multiple buttons containing "Dialog".

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ce7ff28 and 573f975.

Files ignored due to path filters (4)
  • tests/dialog-box/xdesign.spec.ts-snapshots/basic-usage-chromium-win32.png is excluded by !**/*.png
  • tests/dialog-box/xdesign.spec.ts-snapshots/center-chromium-win32.png is excluded by !**/*.png
  • tests/dialog-box/xdesign.spec.ts-snapshots/open-close-events-chromium-win32.png is excluded by !**/*.png
  • tests/dialog-box/xdesign.spec.ts-snapshots/right-dialog-chromium-win32.png is excluded by !**/*.png
Files selected for processing (1)
  • tests/dialog-box/xdesign.spec.ts (1 hunks)

Comment on lines +14 to +22
test('布局示例--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('dialog-box#open-close-events')
const demo = page.locator('#open-close-events')
await demo.getByRole('button', { name: '弹出与关闭事件' }).click()
const body = demo.locator('.tiny-dialog-box')
await expect(body).toBeInViewport()
await expect(body).toHaveScreenshot('open-close-events.png')
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align test name with the tested functionality and consider adding event tests.

The test structure is consistent with the previous test, which is good. However, there are a couple of points to consider:

  1. The test name "布局示例" (Layout example) doesn't match the section being tested ("open-close-events"). Consider renaming the test to reflect the actual functionality being tested.

  2. Despite the section name "open-close-events", the test doesn't actually verify any open or close events. Consider adding assertions to check if these events are fired correctly.

Here's a suggested improvement:

test('Open and close events--UI screenshot', async ({ page }) => {
  page.on('pageerror', (exception) => expect(exception).toBeNull())
  await page.goto('dialog-box#open-close-events')
  const demo = page.locator('#open-close-events')
  
  // Set up event listeners
  const openEventPromise = demo.evaluate(() => new Promise(resolve => {
    document.querySelector('.tiny-dialog-box').addEventListener('open', resolve, { once: true })
  }))
  const closeEventPromise = demo.evaluate(() => new Promise(resolve => {
    document.querySelector('.tiny-dialog-box').addEventListener('close', resolve, { once: true })
  }))
  
  // Open dialog
  await demo.getByRole('button', { name: '弹出与关闭事件' }).click()
  await openEventPromise
  const body = demo.locator('.tiny-dialog-box')
  await expect(body).toBeInViewport()
  await expect(body).toHaveScreenshot('open-close-events-open.png')
  
  // Close dialog
  await demo.getByRole('button', { name: 'Close' }).click()
  await closeEventPromise
  await expect(body).not.toBeInViewport()
  
  // Verify final state
  await expect(page).toHaveScreenshot('open-close-events-closed.png')
})

This improved version tests both the open and close events, as well as the visibility of the dialog box.

Comment on lines +24 to +32
test('右侧弹窗--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('dialog-box#right-dialog')
const demo = page.locator('#right-dialog')
await demo.getByRole('button', { name: '右侧弹窗' }).click()
const body = demo.locator('.is-right-slide')
await expect(body).toBeInViewport()
await expect(body).toHaveScreenshot('right-dialog.png')
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Consider adding a position check for the right-side dialog.

The test case for the right-side dialog is well-structured and consistent with the previous tests. Good job on using the specific '.is-right-slide' class to target the right-side dialog.

To further improve the test, consider adding an assertion to verify that the dialog is actually positioned on the right side of the screen. You can do this by checking the bounding box of the dialog element. Here's a suggested addition:

const dialogBox = demo.locator('.is-right-slide');
const boundingBox = await dialogBox.boundingBox();
const pageWidth = await page.evaluate(() => window.innerWidth);
expect(boundingBox.x).toBeGreaterThan(pageWidth / 2);

This assertion checks that the x-coordinate of the dialog box is in the right half of the screen, confirming its right-side positioning.

Comment on lines +34 to +43
// dialog-box#center
test('标题居中--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('dialog-box#center')
const demo = page.locator('#center')
await demo.getByRole('button', { name: /Dialog/ }).click()
const body = demo.locator('.tiny-dialog-box')
await expect(body).toBeInViewport()
await expect(body).toHaveScreenshot('center.png')
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add specific assertion for centered title and remove unnecessary comment.

The test case for the centered title dialog is structurally consistent with the previous tests, which is good. However, there are a couple of improvements that can be made:

  1. The test name suggests it's checking for a centered title, but there's no specific assertion for this. Consider adding an assertion to verify the title centering.

  2. The comment on line 34 (// dialog-box#center) is unnecessary as it's just repeating the URL. It can be removed to reduce clutter.

Here's a suggested improvement:

test('Centered title--UI screenshot', async ({ page }) => {
  page.on('pageerror', (exception) => expect(exception).toBeNull())
  await page.goto('dialog-box#center')
  const demo = page.locator('#center')
  await demo.getByRole('button', { name: /Dialog/ }).click()
  const body = demo.locator('.tiny-dialog-box')
  await expect(body).toBeInViewport()
  
  // Check if the title is centered
  const title = body.locator('.tiny-dialog-box__header');
  const titleBox = await title.boundingBox();
  const bodyBox = await body.boundingBox();
  expect(titleBox.x + titleBox.width / 2).toBeCloseTo(bodyBox.x + bodyBox.width / 2, 1);
  
  await expect(body).toHaveScreenshot('center.png')
})

This improved version adds an assertion to check if the title is actually centered within the dialog box, and removes the unnecessary comment.

Comment on lines +1 to +44
import { expect, test } from '@playwright/test'

test.describe('dialog-box组件xdesign规范', () => {
test('基本用法--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('dialog-box#basic-usage')
const demo = page.locator('#basic-usage')
await demo.getByRole('button', { name: /Dialog/ }).click()
const body = demo.locator('.tiny-dialog-box')
await expect(body).toBeInViewport()
await expect(body).toHaveScreenshot('basic-usage.png')
})

test('布局示例--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('dialog-box#open-close-events')
const demo = page.locator('#open-close-events')
await demo.getByRole('button', { name: '弹出与关闭事件' }).click()
const body = demo.locator('.tiny-dialog-box')
await expect(body).toBeInViewport()
await expect(body).toHaveScreenshot('open-close-events.png')
})

test('右侧弹窗--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('dialog-box#right-dialog')
const demo = page.locator('#right-dialog')
await demo.getByRole('button', { name: '右侧弹窗' }).click()
const body = demo.locator('.is-right-slide')
await expect(body).toBeInViewport()
await expect(body).toHaveScreenshot('right-dialog.png')
})

// dialog-box#center
test('标题居中--UI截图', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())
await page.goto('dialog-box#center')
const demo = page.locator('#center')
await demo.getByRole('button', { name: /Dialog/ }).click()
const body = demo.locator('.tiny-dialog-box')
await expect(body).toBeInViewport()
await expect(body).toHaveScreenshot('center.png')
})
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding setup/teardown and improving test descriptions.

The overall structure of the test suite is good, with consistent error handling and use of screenshots for visual verification. However, there are a few suggestions for improvement:

  1. Consider adding setup and teardown code using test.beforeEach() and test.afterEach(). This could handle common operations like navigating to the page and setting up error handling, reducing repetition in individual tests.

  2. Make test descriptions more consistent and informative. They should clearly indicate what aspect of the dialog-box is being tested.

  3. Consider parameterizing the tests to reduce code duplication.

Here's an example of how you could refactor the suite:

import { expect, test } from '@playwright/test'

test.describe('dialog-box component xdesign specification', () => {
  test.beforeEach(async ({ page }) => {
    page.on('pageerror', (exception) => expect(exception).toBeNull())
    await page.goto('dialog-box')
  })

  const testCases = [
    { name: 'Basic usage', selector: '#basic-usage', buttonText: 'Dialog', screenshotName: 'basic-usage.png' },
    { name: 'Open and close events', selector: '#open-close-events', buttonText: '弹出与关闭事件', screenshotName: 'open-close-events.png' },
    { name: 'Right-side dialog', selector: '#right-dialog', buttonText: '右侧弹窗', screenshotName: 'right-dialog.png', additionalClass: '.is-right-slide' },
    { name: 'Centered title', selector: '#center', buttonText: 'Dialog', screenshotName: 'center.png' },
  ]

  for (const { name, selector, buttonText, screenshotName, additionalClass } of testCases) {
    test(`${name} - UI screenshot`, async ({ page }) => {
      const demo = page.locator(selector)
      await demo.getByRole('button', { name: buttonText }).click()
      const body = demo.locator(additionalClass ? `.tiny-dialog-box${additionalClass}` : '.tiny-dialog-box')
      await expect(body).toBeInViewport()
      await expect(body).toHaveScreenshot(screenshotName)
    })
  }
})

This refactored version reduces code duplication, improves consistency, and makes it easier to add new test cases in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e playwright
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants