Skip to content

Commit

Permalink
fix(test): fix packages/components tests, remove unnecessary type casts
Browse files Browse the repository at this point in the history
```
yarn
cd packages/components
yarn test
```
->
```
Test Suites: 1 skipped, 64 passed, 64 of 65 total
Tests:       24 skipped, 226 passed, 250 total
Snapshots:   28 passed, 28 total
Time:        34.789 s, estimated 36 s
Ran all test suites.
```

refs LIIKUNTA-651
  • Loading branch information
karisal-anders committed May 21, 2024
1 parent 766b0c4 commit 94e1c09
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
organizationName,
organizerName,
price,
providerContactInfo,
streetAddress,
subEventsResponse,
superEventInternalId,
Expand Down Expand Up @@ -82,26 +83,33 @@ it('should render event info fields', async () => {
{ role: 'heading', name: translations.event.info.labelPrice },
];

itemsByRole.forEach(({ role, name }) => {
expect(screen.getByRole(role, { name })).toBeInTheDocument();
});
for (const { role, name } of itemsByRole) {
expect(await screen.findByRole(role, { name })).toBeInTheDocument();
}

const itemsByText = [
'Ma 22.6.2020, klo 10.00–13.00',
addressLocality,
locationName,
streetAddress,
email,
telephone,
organizationName,
organizerName,
price,
];

itemsByText.forEach(async (item) => {
await screen.findByText(item);
});
});
for (const item of itemsByText) {
expect(await screen.findByText(item)).toBeInTheDocument();
}

// Email and telephone should not be found in the document anymore
expect(screen.queryByText(email)).not.toBeInTheDocument();
expect(screen.queryByText(telephone)).not.toBeInTheDocument();

// Instead the Finnish translation of provider contact info should be visible
expect(await screen.findByText(providerContactInfo.fi)).toBeInTheDocument();
expect(screen.queryByText(providerContactInfo.en)).not.toBeInTheDocument();
expect(screen.queryByText(providerContactInfo.sv)).not.toBeInTheDocument();
}, 20_000);

it('should hide the organizer section when the organizer name is not given', async () => {
const mockEvent = {
Expand All @@ -124,18 +132,13 @@ it('should hide the organizer section when the organizer name is not given', asy
).not.toBeInTheDocument();
});

it('should hide other info section', () => {
const mockEvent = {
it('should hide other info section if no provider contact info, external links or info url', () => {
const mockEvent: EventFields = {
...event,
externalLinks: [],
infoUrl: null,
location: {
...event.location,
email: null,
externalLinks: [],
telephone: null,
},
} as EventFields;
providerContactInfo: null,
};
render(<EventInfo event={mockEvent} />, {
mocks,
});
Expand All @@ -146,12 +149,13 @@ it('should hide other info section', () => {
name: translations.event.info.labelOtherInfo,
})
).not.toBeInTheDocument();
expect(screen.queryByText(email)).not.toBeInTheDocument();
expect(screen.queryByText(telephone)).not.toBeInTheDocument();
expect(screen.queryByText(providerContactInfo.en)).not.toBeInTheDocument();
expect(screen.queryByText(providerContactInfo.fi)).not.toBeInTheDocument();
expect(screen.queryByText(providerContactInfo.sv)).not.toBeInTheDocument();
});

it('should hide other info section registration url from external links', () => {
const mockEvent = {
const mockEvent: EventFields = {
...event,
externalLinks: [
{
Expand All @@ -160,13 +164,7 @@ it('should hide other info section registration url from external links', () =>
},
],
infoUrl: null,
location: {
...event.location,
email: null,
externalLinks: [],
telephone: null,
},
} as EventFields;
};
render(<EventInfo event={mockEvent} />, {
mocks,
});
Expand All @@ -179,18 +177,16 @@ it('should hide other info section registration url from external links', () =>
});

it('should hide the map link from location info if location is internet', () => {
const mockEvent = {
expect(event.location).not.toBeFalsy();
const mockEvent: EventFields = {
...event,
externalLinks: [],
infoUrl: null,
location: {
...event.location,
...event.location!,
id: 'helsinki:internet',
email: null,
externalLinks: [],
telephone: null,
},
} as EventFields;
};
render(<EventInfo event={mockEvent} />, {
mocks,
});
Expand Down Expand Up @@ -298,29 +294,23 @@ describe('OrganizationInfo', () => {
])(
'should show correct provider link text on event/hobby detail page',
async ({ eventTypeId, expectedLinkText }) => {
render(
<EventInfo event={{ ...event, typeId: eventTypeId } as EventFields} />,
{
mocks: mocksWithSubEvents,
routes: ['/fi/kurssit/test'],
}
);
render(<EventInfo event={{ ...event, typeId: eventTypeId }} />, {
mocks: mocksWithSubEvents,
routes: ['/fi/kurssit/test'],
});
await waitFor(() => {
expect(screen.getByText(expectedLinkText)).toBeInTheDocument();
});
}
);

it.each(Object.keys(EventTypeId))(
it.each(Object.values(EventTypeId))(
'should show correct provider link on event/hobby detail page',
async (eventTypeId) => {
render(
<EventInfo event={{ ...event, typeId: eventTypeId } as EventFields} />,
{
mocks,
routes: ['/fi/kurssit/test'],
}
);
render(<EventInfo event={{ ...event, typeId: eventTypeId }} />, {
mocks,
routes: ['/fi/kurssit/test'],
});
const getPublisherLink = () => {
const publisherLinkElement: HTMLElement =
screen.getByTestId('publisherLink');
Expand Down Expand Up @@ -369,10 +359,10 @@ describe('superEvent', () => {
superEvent: { internalId: superEventInternalId },
typeId: EventTypeId.General,
});
const superEventResponse = {
const superEventResponse: SuperEventResponse = {
data: superEvent,
status: 'resolved',
} as SuperEventResponse;
};
const { router } = render(
<EventInfo event={event} superEvent={superEventResponse} />,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import type {
EventListQueryVariables,
EventListResponse,
Meta,
LocalizedObject,
} from '../../../../../../src/types/generated/graphql';
import type { ValidLocalizedObject } from '../../../../../types';

export const organizationId = '1';
export const organizationName = 'Organization name';
Expand All @@ -36,6 +38,12 @@ export const startTime = '2020-06-22T07:00:00.000000Z';
export const endTime = '2020-06-22T10:00:00.000000Z';
export const email = 'test@email.com';
export const telephone = '0441234567';
export const providerContactInfo: ValidLocalizedObject = {
__typename: 'LocalizedObject',
fi: 'Finnish provider contact info',
en: 'English provider contact info',
sv: 'Swedish provider contact info',
} satisfies LocalizedObject;
export const addressLocality = 'Helsinki';
export const neighborhood = 'Malmi';
export const locationName = 'Location name';
Expand All @@ -48,12 +56,13 @@ export const remainingAttendeeCapacity = 5;
export const audienceMinAge = '5';
export const audienceMaxAge = '15';
export const organizerName = 'provider organisation';
export const event = fakeEvent({
export const event: EventFieldsFragment = fakeEvent({
audienceMinAge,
audienceMaxAge,
startTime,
endTime,
provider: { fi: organizerName },
providerContactInfo,
publisher: organizationId,
location: {
divisions: [{ name: { fi: neighborhood }, type: 'neighborhood' }],
Expand All @@ -72,7 +81,7 @@ export const event = fakeEvent({
fakeTargetGroup({ name: fakeLocalizedObject(targetGroup) })
),
typeId: EventTypeId.Course,
}) as EventFieldsFragment;
});

export const meta: Meta = {
count: 20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import type { ShareLinkProps } from '../types';
const renderComponent = (props: ShareLinkProps) =>
render(<FacebookShareLink {...props} />);

it('should apply aria label', () => {
it('should apply aria label', async () => {
const sharedLink = 'https://helsinki.fi/some/';
renderComponent({ sharedLink });

expect(screen.getByLabelText(/Jaa Facebookissa/)).toBeInTheDocument();
expect(
await screen.findByRole('button', { name: /Jaa Facebookissa/ })
).toBeInTheDocument();
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import type { ShareLinkProps } from '../types';
const renderComponent = (props: ShareLinkProps) =>
render(<LinkedInShareLink {...props} />);

it('should apply aria label', () => {
it('should apply aria label', async () => {
const sharedLink = 'https://helsinki.fi/some/';
renderComponent({ sharedLink });

expect(screen.getByLabelText(/Jaa LinkedInissä/i)).toBeInTheDocument();
expect(
await screen.findByRole('button', { name: /Jaa LinkedInissä/i })
).toBeInTheDocument();
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ShareLinks from '../ShareLinks';
const renderComponent = (props: ShareLinksProps) =>
render(<ShareLinks {...props} />);

it('should have discoverable link address copy button as well as Facebook, Twitter and LinkedIn share links', () => {
it('should have discoverable link address copy button as well as Facebook, Twitter and LinkedIn share link buttons', async () => {
renderComponent({ title: 'Jaa tapahtuma' });
const shareLinkLabelsFI = [
/Kopioi linkin osoite/,
Expand All @@ -15,7 +15,9 @@ it('should have discoverable link address copy button as well as Facebook, Twitt
/Jaa LinkedInissä/,
];

shareLinkLabelsFI.forEach((label) => {
expect(screen.getByLabelText(label)).not.toStrictEqual(null);
});
for (const label of shareLinkLabelsFI) {
expect(
await screen.findByRole('button', { name: label })
).toBeInTheDocument();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import TwitterShareLink from '../TwitterShareLink';
const renderComponent = (props: { sharedLink: string }) =>
render(<TwitterShareLink {...props} />);

it('should apply aria label', () => {
it('should apply aria label', async () => {
const sharedLink = 'https://helsinki.fi/some/';
renderComponent({ sharedLink });

expect(screen.getByLabelText(/Jaa Twitterissä/)).toBeInTheDocument();
expect(
await screen.findByRole('button', { name: /Jaa Twitterissä/ })
).toBeInTheDocument();
});
9 changes: 8 additions & 1 deletion packages/components/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import type { NextRouter } from 'next/router';
import type React from 'react';
import type { APP_LANGUAGES } from '../constants';
import type { EventFields } from './event-types';
import type { Venue } from './generated/graphql';
import type { LocalizedObject, Venue } from './generated/graphql';
export type AppLanguage = (typeof APP_LANGUAGES)[number];

export type AutosuggestType = 'keyword' | 'text';

export type ValidLocalizedObject = {
__typename: LocalizedObject['__typename'];
en: NonNullable<LocalizedObject['en']>;
fi: NonNullable<LocalizedObject['fi']>;
sv: NonNullable<LocalizedObject['sv']>;
};

/* eslint-enable @typescript-eslint/naming-convention */

export type Option = {
Expand Down

0 comments on commit 94e1c09

Please sign in to comment.