Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions packages/shared/src/components/Feed.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1172,15 +1172,14 @@ describe('Feed logged in', () => {
'Off-topic or wrong tags',
);
fireEvent.click(irrelevantTagsBtn);
const submitBtn = await screen.findByText('Submit report');
const submitBtn = await screen.findByRole('button', {
name: 'Submit report',
});
expect(submitBtn).toBeDisabled();
const javascriptElements = await screen.findAllByText('#javascript');
const javascriptBtn = javascriptElements.find(
(item) => item.tagName === 'BUTTON',
);
fireEvent.click(
getRequiredValue(javascriptBtn, 'Expected javascript report tag button'),
);
const javascriptBtn = await screen.findByRole('button', {
name: '#javascript',
});
fireEvent.click(javascriptBtn);
expect(submitBtn).toBeEnabled();
fireEvent.click(submitBtn);

Expand Down Expand Up @@ -1211,14 +1210,13 @@ describe('Feed logged in', () => {
'Off-topic or wrong tags',
);
fireEvent.click(irrelevantTagsBtn);
const submitBtn = await screen.findByText('Submit report');
const javascriptElements = await screen.findAllByText('#javascript');
const javascriptBtn = javascriptElements.find(
(item) => item.tagName === 'BUTTON',
);
fireEvent.click(
getRequiredValue(javascriptBtn, 'Expected javascript report tag button'),
);
const submitBtn = await screen.findByRole('button', {
name: 'Submit report',
});
const javascriptBtn = await screen.findByRole('button', {
name: '#javascript',
});
fireEvent.click(javascriptBtn);
expect(submitBtn).toBeEnabled();

const brokenLinkBtn = await screen.findByText('Broken link');
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/src/components/buttons/Button.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ describe('Button', () => {
expect(await screen.findByTestId('buttonLoader')).toBeInTheDocument();
});

it('keeps the same label wrapper when toggling loading', () => {
const { rerender } = render(
<Button variant={ButtonVariant.Primary}>Button</Button>,
);

const initialLabel = screen.getByRole('button').querySelector('.btn-label');

expect(initialLabel).toBeInTheDocument();
expect(initialLabel).not.toHaveClass('invisible');

rerender(
<Button variant={ButtonVariant.Primary} loading>
Button
</Button>,
);

const loadingLabel = screen.getByRole('button').querySelector('.btn-label');

expect(loadingLabel).toBe(initialLabel);
expect(loadingLabel).toHaveClass('invisible');
});

it('should set aria-pressed when pressed is true', async () => {
renderComponent({ children: 'Button', pressed: true });
expect(await screen.findByRole('button')).toHaveAttribute(
Expand Down
9 changes: 7 additions & 2 deletions packages/shared/src/components/buttons/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ function ButtonComponent<TagName extends AllowedTags>(
}: ButtonProps<TagName>,
ref?: Ref<ButtonElementType<TagName>>,
): ReactElement {
const iconOnly = icon && isNullOrUndefined(children);
const hasChildren = !isNullOrUndefined(children);
const iconOnly = icon && !hasChildren;
const getIconWithSize = useGetIconWithSize(
size,
iconOnly ?? false,
Expand Down Expand Up @@ -126,7 +127,11 @@ function ButtonComponent<TagName extends AllowedTags>(
iconPosition,
) &&
getIconWithSize(icon, iconSecondaryOnHover ? isHovering : false)}
{loading ? <span className="invisible">{children}</span> : children}
{hasChildren && (
<span className={classNames('btn-label', loading && 'invisible')}>
{children}
</span>
)}
{icon &&
iconPosition === ButtonIconPosition.Right &&
getIconWithSize(icon, iconSecondaryOnHover ? isHovering : false)}
Expand Down
24 changes: 19 additions & 5 deletions packages/shared/src/components/modals/user/CookieConsentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ export const CookieConsentModal = ({
}: CookieConsentModalProps): ReactElement => {
const { onRequestClose } = modalProps;

const onAcceptPreferences = (e: React.FormEvent) => {
e.preventDefault();
// get the form
const formData = new FormData((e.target as HTMLInputElement).form);
const submitAcceptedPreferences = (form: HTMLFormElement) => {
const formData = new FormData(form);
const formProps = Object.fromEntries(formData);
const keys = Object.keys(formProps);
const acceptedConsents = keys.filter(
Expand All @@ -47,6 +45,11 @@ export const CookieConsentModal = ({
(option) => !acceptedConsents.includes(option),
);
onAcceptCookies(acceptedConsents, rejectedConsents);
};

const onAcceptPreferences = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
submitAcceptedPreferences(e.currentTarget);

onRequestClose(null);
};
Expand All @@ -61,6 +64,17 @@ export const CookieConsentModal = ({
onRequestClose(e);
};

const onSavePreferences = (e: React.MouseEvent<HTMLButtonElement>) => {
const { form } = e.currentTarget;

if (!form) {
throw new Error('Cookie consent form is missing');
}

submitAcceptedPreferences(form);
onRequestClose(e);
};

return (
<Modal
{...modalProps}
Expand Down Expand Up @@ -120,7 +134,7 @@ export const CookieConsentModal = ({
<Button
variant={ButtonVariant.Primary}
size={ButtonSize.Small}
onClick={onAcceptPreferences}
onClick={onSavePreferences}
form="consent_form"
className="ml-auto"
type="submit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ it('should show number comments', async () => {

it('should set feeling lucky link to the first post', async () => {
renderComponent();
const el = await screen.findByText(`I'm feeling lucky`);
// eslint-disable-next-line testing-library/no-node-access
const el = await screen.findByRole('link', { name: `I'm feeling lucky` });
expect(el).toHaveAttribute('href', defaultPosts[0].commentsPermalink);
});
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,8 @@ const renderComponent = (props = {}, initialState: InitialState = {}) => {
describe('FunnelPricingV2', () => {
let dateMock: DateMock;
const initialDate = new Date('2023-01-01T00:00:00Z');
const getProceedButton = (): HTMLElement => {
const proceedButton = screen
.getAllByText('Get my plan')
.filter((button) => button.tagName === 'BUTTON')
.at(0);

if (!proceedButton) {
throw new Error('Expected pricing CTA button');
}

return proceedButton;
};
const getProceedButton = (): HTMLElement =>
screen.getAllByRole('button', { name: 'Get my plan' })[0];

const getMonthlyPlanRadio = (): HTMLElement => {
const monthlyPlan = screen
Expand Down
4 changes: 2 additions & 2 deletions packages/webapp/__tests__/PostPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,8 @@ describe('downvote flow', () => {

it('should prevent user to click block if no tags are selected', async () => {
await prepareDownvote();
const block = await screen.findByText<HTMLButtonElement>('Block');
expect(block?.disabled).toBe(true);
const block = await screen.findByRole('button', { name: 'Block' });
expect(block.disabled).toBe(true);
});

it('should display the option to never see the selection again if close panel', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/webapp/components/banner/CookieBanner.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('CookieBanner outside GDPR', () => {
it('should render just a single button to accept all when outside GDPR coverage', async () => {
renderComponent();
await nextTick();
const el = await screen.findByText('I understand');
const el = await screen.findByRole('button', { name: 'I understand' });
expect(el.tagName).toBe('BUTTON');
});

Expand Down Expand Up @@ -83,7 +83,7 @@ describe('CookieBanner outside GDPR', () => {
renderComponent();

await screen.findByTestId('cookie_content');
const button = await screen.findByText('I understand');
const button = await screen.findByRole('button', { name: 'I understand' });
await act(() => fireEvent.click(button));
await nextTick();
const cookies = getCookies([GdprConsentKey.Necessary]);
Expand Down
Loading