-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Cover block: Add integration tests #45409
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
042428d
Add more block toolbar tests, and initial toolspanel test
glendaviesnz fa0a316
Add more tests and switch to userEvent instead of fireEvent
glendaviesnz 2e41650
remove inadvertent change
glendaviesnz 4fdb78b
Change the way the controls are mocked
glendaviesnz 214c315
Finish off the inspector control tests
glendaviesnz 5654827
Update comment
glendaviesnz 019d5a3
Add some tests for edit cmponent
glendaviesnz 80316b6
remove inadvertent commit
glendaviesnz 6360966
Remove jest advance timers
glendaviesnz 69b066e
Fix broken test
glendaviesnz 249f9b1
Wrap the Cover Edit component in a SlotProvider for testing
glendaviesnz 2aa6d55
Woop Woop - fire up an editor instance to integration test a block Ed…
glendaviesnz fd77c1f
set up function to prevent duplication of user events
glendaviesnz 0117b24
Replace Cover e2e tests than can be replaced as integration tests
glendaviesnz 3043e47
Tidy up integration test editor
glendaviesnz 65bfc75
move to integration tests folder and add wrapper to fix issue with as…
glendaviesnz 43d855d
Tidy up test editor setup
glendaviesnz 15ac92a
Move inspector control tests to use new integration editor
glendaviesnz 8af2060
Move block toolbar tests to new integration test editor
glendaviesnz d56dc26
Remove unnecessary useEffect
glendaviesnz fbc56c4
Attempt to fix CI error
glendaviesnz 408e22e
Add some debugging for CI issue
glendaviesnz 21f97c7
Revert "Add some debugging for CI issue"
glendaviesnz 4697a69
Comment out media related tests to see if that fixes test failures
glendaviesnz 6f19aef
Put url related tests back in
glendaviesnz 29bf6ba
Add await to the block creation
glendaviesnz 19552d1
try different way of wrapping async call to see if that fixes CI issue
glendaviesnz 7bf0bce
Revert "try different way of wrapping async call to see if that fixes…
glendaviesnz a2f16fa
Add polyfill for missing string method on CI
glendaviesnz 431b2ba
remove optional chain that wasn't actually needed
glendaviesnz a293716
changes from code review
glendaviesnz f201a3b
Add some doc comments to the integration test editor methods
glendaviesnz 50a264a
changes from code review
glendaviesnz a58601f
Move some of the block initialisation into the editor component to av…
glendaviesnz 7a2b353
Fix type in comment
glendaviesnz b0f0746
Move the setup to initialise method rather than an effect
glendaviesnz 8948961
Add doc block comment
glendaviesnz c5fccc6
Fixes from review
glendaviesnz 93148ca
Remove the class selectors that can use `img` instead
glendaviesnz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,324 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { screen, fireEvent, act, within } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
initializeEditor, | ||
selectBlock, | ||
} from 'test/integration/helpers/integration-test-editor'; | ||
|
||
async function setup( attributes ) { | ||
const testBlock = { name: 'core/cover', attributes }; | ||
return initializeEditor( testBlock ); | ||
} | ||
|
||
async function createAndSelectBlock() { | ||
await userEvent.click( | ||
screen.getByRole( 'button', { | ||
name: 'Color: Black', | ||
} ) | ||
); | ||
await userEvent.click( | ||
screen.getByRole( 'button', { | ||
name: 'Select Cover', | ||
} ) | ||
); | ||
} | ||
|
||
describe( 'Cover block', () => { | ||
describe( 'Editor canvas', () => { | ||
test( 'shows placeholder if background image and color not set', async () => { | ||
await setup(); | ||
|
||
expect( | ||
screen.getByRole( 'group', { | ||
name: 'To edit this block, you need permission to upload media.', | ||
} ) | ||
).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'can set overlay color using color picker on block placeholder', async () => { | ||
const { container } = await setup(); | ||
const colorPicker = screen.getByRole( 'button', { | ||
name: 'Color: Black', | ||
} ); | ||
await userEvent.click( colorPicker ); | ||
const color = colorPicker.style.backgroundColor; | ||
expect( | ||
screen.queryByRole( 'group', { | ||
name: 'To edit this block, you need permission to upload media.', | ||
} ) | ||
).not.toBeInTheDocument(); | ||
|
||
// eslint-disable-next-line testing-library/no-node-access | ||
const overlay = container.getElementsByClassName( | ||
'wp-block-cover__background' | ||
); | ||
expect( overlay[ 0 ] ).toHaveStyle( | ||
`background-color: ${ color }` | ||
); | ||
} ); | ||
|
||
test( 'can have the title edited', async () => { | ||
await setup(); | ||
|
||
await userEvent.click( | ||
screen.getByRole( 'button', { | ||
name: 'Color: Black', | ||
} ) | ||
); | ||
|
||
const title = screen.getByLabelText( 'Empty block;', { | ||
exact: false, | ||
} ); | ||
await userEvent.click( title ); | ||
await userEvent.keyboard( 'abc' ); | ||
expect( title ).toHaveTextContent( 'abc' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'Block toolbar', () => { | ||
test( 'full height toggle sets minHeight style attribute to 100vh when clicked', async () => { | ||
await setup(); | ||
await createAndSelectBlock(); | ||
|
||
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveStyle( | ||
'min-height: 100vh;' | ||
); | ||
|
||
await userEvent.click( | ||
screen.getByLabelText( 'Toggle full height' ) | ||
); | ||
|
||
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveStyle( | ||
'min-height: 100vh;' | ||
); | ||
} ); | ||
|
||
test( 'content position button sets content position', async () => { | ||
await setup(); | ||
await createAndSelectBlock(); | ||
|
||
await userEvent.click( | ||
screen.getByLabelText( 'Change content position' ) | ||
); | ||
|
||
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveClass( | ||
'has-custom-content-position' | ||
); | ||
|
||
await act( async () => | ||
glendaviesnz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
within( screen.getByRole( 'grid' ) ) | ||
.getByRole( 'gridcell', { | ||
name: 'top left', | ||
} ) | ||
.focus() | ||
); | ||
|
||
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass( | ||
'has-custom-content-position' | ||
); | ||
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass( | ||
'is-position-top-left' | ||
); | ||
} ); | ||
} ); | ||
|
||
describe( 'Inspector controls', () => { | ||
describe( 'Media settings', () => { | ||
test( 'does not display media settings panel if url is not set', async () => { | ||
await setup(); | ||
expect( | ||
screen.queryByRole( 'button', { | ||
name: 'Media settings', | ||
} ) | ||
).not.toBeInTheDocument(); | ||
} ); | ||
test( 'displays media settings panel if url is set', async () => { | ||
await setup( { | ||
url: 'http://localhost/my-image.jpg', | ||
} ); | ||
|
||
await selectBlock( 'Block: Cover' ); | ||
expect( | ||
screen.getByRole( 'button', { | ||
name: 'Media settings', | ||
} ) | ||
).toBeInTheDocument(); | ||
} ); | ||
} ); | ||
|
||
test( 'sets hasParallax attribute to true if fixed background toggled', async () => { | ||
await setup( { | ||
url: 'http://localhost/my-image.jpg', | ||
} ); | ||
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveClass( | ||
'has-parallax' | ||
); | ||
await selectBlock( 'Block: Cover' ); | ||
await userEvent.click( | ||
screen.getByLabelText( 'Fixed background' ) | ||
); | ||
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass( | ||
'has-parallax' | ||
); | ||
} ); | ||
|
||
test( 'sets isRepeated attribute to true if repeated background toggled', async () => { | ||
await setup( { | ||
url: 'http://localhost/my-image.jpg', | ||
} ); | ||
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveClass( | ||
'is-repeated' | ||
); | ||
await selectBlock( 'Block: Cover' ); | ||
await userEvent.click( | ||
screen.getByLabelText( 'Repeated background' ) | ||
); | ||
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass( | ||
'is-repeated' | ||
); | ||
} ); | ||
|
||
test( 'sets left focalPoint attribute when focal point values changed', async () => { | ||
await setup( { | ||
url: 'http://localhost/my-image.jpg', | ||
} ); | ||
|
||
await selectBlock( 'Block: Cover' ); | ||
await userEvent.clear( screen.getByLabelText( 'Left' ) ); | ||
await userEvent.type( screen.getByLabelText( 'Left' ), '100' ); | ||
|
||
expect( | ||
within( screen.getByLabelText( 'Block: Cover' ) ).getByRole( | ||
'img' | ||
) | ||
).toHaveStyle( 'object-position: 100% 50%;' ); | ||
} ); | ||
|
||
test( 'sets alt attribute if text entered in alt text box', async () => { | ||
await setup( { | ||
url: 'http://localhost/my-image.jpg', | ||
} ); | ||
|
||
await selectBlock( 'Block: Cover' ); | ||
await userEvent.type( | ||
screen.getByLabelText( 'Alt text (alternative text)' ), | ||
'Me' | ||
); | ||
expect( screen.getByAltText( 'Me' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'clears media when clear media button clicked', async () => { | ||
await setup( { | ||
url: 'http://localhost/my-image.jpg', | ||
} ); | ||
|
||
await selectBlock( 'Block: Cover' ); | ||
expect( | ||
within( screen.getByLabelText( 'Block: Cover' ) ).getByRole( | ||
'img' | ||
) | ||
).toBeInTheDocument(); | ||
|
||
await userEvent.click( | ||
screen.getByRole( 'button', { | ||
name: 'Clear Media', | ||
} ) | ||
); | ||
expect( | ||
within( screen.queryByLabelText( 'Block: Cover' ) ).queryByRole( | ||
'img' | ||
) | ||
).not.toBeInTheDocument(); | ||
} ); | ||
|
||
describe( 'Color panel', () => { | ||
test( 'applies selected opacity to block when number control value changed', async () => { | ||
const { container } = await setup(); | ||
|
||
await createAndSelectBlock(); | ||
|
||
// eslint-disable-next-line testing-library/no-node-access | ||
const overlay = container.getElementsByClassName( | ||
'wp-block-cover__background' | ||
); | ||
|
||
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-100' ); | ||
|
||
await userEvent.click( | ||
screen.getByRole( 'tab', { | ||
name: 'Styles', | ||
} ) | ||
); | ||
|
||
fireEvent.change( | ||
screen.getByRole( 'spinbutton', { | ||
name: 'Overlay opacity', | ||
} ), | ||
{ | ||
target: { value: '40' }, | ||
} | ||
); | ||
|
||
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-40' ); | ||
} ); | ||
|
||
test( 'applies selected opacity to block when slider moved', async () => { | ||
const { container } = await setup(); | ||
|
||
await createAndSelectBlock(); | ||
|
||
// eslint-disable-next-line testing-library/no-node-access | ||
const overlay = container.getElementsByClassName( | ||
'wp-block-cover__background' | ||
); | ||
|
||
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-100' ); | ||
|
||
await userEvent.click( | ||
screen.getByRole( 'tab', { | ||
name: 'Styles', | ||
} ) | ||
); | ||
|
||
fireEvent.change( | ||
screen.getByRole( 'slider', { | ||
name: 'Overlay opacity', | ||
} ), | ||
{ target: { value: 30 } } | ||
); | ||
|
||
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-30' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'Dimensions panel', () => { | ||
test( 'sets minHeight attribute when number control value changed', async () => { | ||
await setup(); | ||
await createAndSelectBlock(); | ||
await userEvent.click( | ||
screen.getByRole( 'tab', { | ||
name: 'Styles', | ||
} ) | ||
); | ||
await userEvent.clear( | ||
screen.getByLabelText( 'Minimum height of cover' ) | ||
); | ||
await userEvent.type( | ||
screen.getByLabelText( 'Minimum height of cover' ), | ||
'300' | ||
); | ||
|
||
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveStyle( | ||
'min-height: 300px;' | ||
); | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still have class name selectors here (and a couple more below), do we also want to change these as well? Or is it a different case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the ones that related to the
img
but the remaining ones are just a span - I can't see another way of getting these other than adding adata-testid
or something, so maybe we should leave them as is until there is a resolution to that question about whether they should be stripped from production code.