|
| 1 | +import assert from 'node:assert' |
| 2 | +import { afterEach, beforeEach, describe, it } from 'node:test' |
| 3 | +import * as React from 'react' |
| 4 | + |
| 5 | +import { BranchAlreadyUpToDate } from '../../../src/ui/banners/branch-already-up-to-date-banner' |
| 6 | +import { Banner } from '../../../src/ui/banners/banner' |
| 7 | +import { CherryPickUndone } from '../../../src/ui/banners/cherry-pick-undone' |
| 8 | +import { SuccessBanner } from '../../../src/ui/banners/success-banner' |
| 9 | +import { |
| 10 | + advanceTimersBy, |
| 11 | + enableTestTimers, |
| 12 | + resetTestTimers, |
| 13 | +} from '../../helpers/ui/timers' |
| 14 | +import { fireEvent, render, screen } from '../../helpers/ui/render' |
| 15 | + |
| 16 | +describe('banner surfaces', () => { |
| 17 | + beforeEach(() => { |
| 18 | + enableTestTimers(['setTimeout']) |
| 19 | + }) |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + resetTestTimers() |
| 23 | + }) |
| 24 | + |
| 25 | + it('focuses the first suitable banner element and auto-dismisses on focus out', () => { |
| 26 | + let dismissed = 0 |
| 27 | + |
| 28 | + function onDismissed() { |
| 29 | + dismissed++ |
| 30 | + } |
| 31 | + |
| 32 | + const view = render( |
| 33 | + <Banner id="test-banner" timeout={500} onDismissed={onDismissed}> |
| 34 | + <a href="https://example.com/help">Learn more</a> |
| 35 | + </Banner> |
| 36 | + ) |
| 37 | + |
| 38 | + const banner = view.container.querySelector('#test-banner.banner') |
| 39 | + const link = screen.getByRole('link', { name: 'Learn more' }) |
| 40 | + const dismissButton = screen.getByRole('button', { |
| 41 | + name: 'Dismiss this message', |
| 42 | + }) |
| 43 | + |
| 44 | + assert.notEqual(banner, null) |
| 45 | + assert.ok(dismissButton) |
| 46 | + |
| 47 | + advanceTimersBy(200) |
| 48 | + |
| 49 | + assert.equal(document.activeElement, link) |
| 50 | + |
| 51 | + fireEvent.focusOut(link, { relatedTarget: document.body }) |
| 52 | + |
| 53 | + advanceTimersBy(500) |
| 54 | + |
| 55 | + assert.equal(dismissed, 1) |
| 56 | + }) |
| 57 | + |
| 58 | + it('renders success banner content and triggers dismiss plus undo from the undo link', () => { |
| 59 | + let dismissed = 0 |
| 60 | + let undone = 0 |
| 61 | + |
| 62 | + function onDismissed() { |
| 63 | + dismissed++ |
| 64 | + } |
| 65 | + |
| 66 | + function onUndo() { |
| 67 | + undone++ |
| 68 | + } |
| 69 | + |
| 70 | + render( |
| 71 | + <SuccessBanner timeout={750} onDismissed={onDismissed} onUndo={onUndo}> |
| 72 | + Branch renamed successfully. |
| 73 | + </SuccessBanner> |
| 74 | + ) |
| 75 | + |
| 76 | + const undoButton = screen.getByRole('button', { name: 'Undo' }) |
| 77 | + |
| 78 | + assert.ok(screen.getByText('Branch renamed successfully.')) |
| 79 | + assert.notEqual(document.querySelector('.success-contents'), null) |
| 80 | + assert.notEqual(document.querySelector('.green-circle .check-icon'), null) |
| 81 | + |
| 82 | + fireEvent.click(undoButton) |
| 83 | + |
| 84 | + assert.equal(dismissed, 1) |
| 85 | + assert.equal(undone, 1) |
| 86 | + }) |
| 87 | + |
| 88 | + it('renders branch up-to-date banner messages with and without the compared branch', () => { |
| 89 | + function onDismissed() {} |
| 90 | + |
| 91 | + const view = render( |
| 92 | + <BranchAlreadyUpToDate |
| 93 | + ourBranch="main" |
| 94 | + theirBranch="origin/main" |
| 95 | + onDismissed={onDismissed} |
| 96 | + /> |
| 97 | + ) |
| 98 | + |
| 99 | + assert.ok(screen.getByText('main')) |
| 100 | + assert.ok(screen.getByText('origin/main')) |
| 101 | + assert.ok( |
| 102 | + view.container.textContent?.includes('is already up to date with') |
| 103 | + ) |
| 104 | + |
| 105 | + view.rerender( |
| 106 | + <BranchAlreadyUpToDate ourBranch="release" onDismissed={onDismissed} /> |
| 107 | + ) |
| 108 | + |
| 109 | + assert.ok(screen.getByText('release')) |
| 110 | + assert.ok(view.container.textContent?.includes('is already up to date')) |
| 111 | + }) |
| 112 | + |
| 113 | + it('renders cherry-pick undone messages with singular and plural commit copy', () => { |
| 114 | + function onDismissed() {} |
| 115 | + |
| 116 | + const view = render( |
| 117 | + <CherryPickUndone |
| 118 | + countCherryPicked={1} |
| 119 | + targetBranchName="main" |
| 120 | + onDismissed={onDismissed} |
| 121 | + /> |
| 122 | + ) |
| 123 | + |
| 124 | + assert.ok( |
| 125 | + view.container.textContent?.includes( |
| 126 | + 'Cherry-pick undone. Successfully removed the 1 copied commit from' |
| 127 | + ) |
| 128 | + ) |
| 129 | + assert.ok(screen.getByText('main')) |
| 130 | + |
| 131 | + view.rerender( |
| 132 | + <CherryPickUndone |
| 133 | + countCherryPicked={3} |
| 134 | + targetBranchName="release" |
| 135 | + onDismissed={onDismissed} |
| 136 | + /> |
| 137 | + ) |
| 138 | + |
| 139 | + assert.ok( |
| 140 | + view.container.textContent?.includes( |
| 141 | + 'Cherry-pick undone. Successfully removed the 3 copied commits from' |
| 142 | + ) |
| 143 | + ) |
| 144 | + assert.ok(screen.getByText('release')) |
| 145 | + }) |
| 146 | +}) |
0 commit comments