-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block unit tests break when testing a gutenberg block as a unit. #37988
Comments
RCA: It looks like when testing the components like so, the const { blockType, attributes } = blockPropsProvider;
Fix: import React from 'react';
import { shallow } from 'enzyme';
import { getSaveElement } from '@wordpress/blocks';
import Component from '../save';
describe('Component Save', () => {
let wrapper;
let ATTR;
beforeAll(() => {
ATTR = {
backgroundColor: 'red-panda'
};
getSaveElement({ name: 'component', save: Component }, ATTR);
wrapper = shallow(<Component attributes={ATTR} />);
});
it('applies backgroundColor', () => {
expect(wrapper.find('.red-panda').exists()).to.equal(true);
});
}); the fix is to call I'll create a PR shortly to address this. |
Hey @whizzzkid, We removed I wonder if using Let me know if that makes sense or if you need any help trying it out. |
I am running into this same error while writing snapshot tests with
I have a similar block structure as @whizzzkid with export default function Save() {
const blockProps = useBlockProps.save();
return (
<section { ...blockProps }>
<InnerBlocks.Content />
</section>
);
} I've tried calling import {
render,
} from '@testing-library/react';
import { getSaveElement } from '@wordpress/blocks';
import Save from '../../../blocks/chapter/save';
describe('Save component', () => {
let el;
beforeAll(() => {
const attributes = { value: 'randomvalue' }
getSaveElement({ name: 'my-block-name', save: Save }, attributes);
const { baseElement } = render(
<Save
{...{
attributes,
clientId: 'random-id',
className: 'wp-block-my-block-name',
}}
/>
);
el = baseElement;
});
it('matches snapshot', () => {
expect( el ).toMatchSnapshot();
});
)}; Any suggestions? Thanks! |
Apologies for the radio silence @tyxla, I got occupied with other things and didn't get time to create that PR. @amberjones the interim fix was mentioned here: #37988 (comment) that worked for us, however did you try passing args to Save like my example? |
No worries @whizzzkid! No rush, but let me know if you need anything in the meantime.
Hey @amberjones, I've tried creating the With regards to @whizzzkid's I'd like to note again as I noted earlier, that we recently removed Enzyme and it's recommended to use |
Description
After upgrade, we noticed our custom components started breaking. The reason for that was the wrapper API changed and now we need to access the wrapper using
useBlockProps
anduseBlockProp.save
. This fixed the component and it started working as expected. But this broke our unit tests with the error:Step-by-step reproduction instructions
Say we create a component, where the
save
function looks like so:To test this, we can now render it and test it like so:
But this results in the error mentioned above.
Screenshots, screen recording, code snippet
No response
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: