Skip to content

Commit

Permalink
tests adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Skwierczyński committed Apr 23, 2024
1 parent eead875 commit 24989b2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useQuery } from '@apollo/client';
import { gql } from '@sb/webapp-api-client';
import { fillCommonQueryWithUser } from '@sb/webapp-api-client/tests/factories';
import { currentUserFactory, fillCommonQueryWithUser } from '@sb/webapp-api-client/tests/factories';
import { composeMockedQueryResult } from '@sb/webapp-api-client/tests/utils';
import { trackEvent } from '@sb/webapp-core/services/analytics';
import { getLocalePath } from '@sb/webapp-core/utils';
import { tenantFactory } from '@sb/webapp-tenants/tests/factories/tenant';
import { screen, waitFor } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { Route, Routes, useParams } from 'react-router';
Expand All @@ -23,6 +24,8 @@ const crudDemoItemListItemTestQuery = gql(/* GraphQL */ `

jest.mock('@sb/webapp-core/services/analytics');

const tenantId = 'tenantId';

describe('CrudDemoItemListItem: Component', () => {
const EditRouteMock = () => {
const params = useParams<{ id: string }>();
Expand Down Expand Up @@ -51,10 +54,18 @@ describe('CrudDemoItemListItem: Component', () => {
};

it('should render link to details page', async () => {
const item = { id: 'test-id', name: 'demo item name' };
const item = { id: 'test-id', name: 'demo item name', tenantId };

const apolloMocks = [
fillCommonQueryWithUser(),
fillCommonQueryWithUser(
currentUserFactory({
tenants: [
tenantFactory({
id: tenantId,
}),
],
})
),
composeMockedQueryResult(crudDemoItemListItemTestQuery, {
data: {
item: {
Expand All @@ -72,10 +83,18 @@ describe('CrudDemoItemListItem: Component', () => {
});

it('should render link to edit form', async () => {
const item = { id: 'test-id', name: 'demo item name' };
const item = { id: 'test-id', name: 'demo item name', tenantId };

const apolloMocks = [
fillCommonQueryWithUser(),
fillCommonQueryWithUser(
currentUserFactory({
tenants: [
tenantFactory({
id: tenantId,
}),
],
})
),
composeMockedQueryResult(crudDemoItemListItemTestQuery, {
data: {
item: {
Expand All @@ -94,10 +113,18 @@ describe('CrudDemoItemListItem: Component', () => {
expect(screen.getByText('Crud demo item edit mock test-id')).toBeInTheDocument();
});
it('should delete item', async () => {
const item = { id: 'test-id', name: 'demo item name' };
const item = { id: 'test-id', name: 'demo item name', tenantId };

const apolloMocks = [
fillCommonQueryWithUser(),
fillCommonQueryWithUser(
currentUserFactory({
tenants: [
tenantFactory({
id: tenantId,
}),
],
})
),
composeMockedQueryResult(crudDemoItemListItemTestQuery, {
data: {
item: {
Expand All @@ -114,7 +141,7 @@ describe('CrudDemoItemListItem: Component', () => {
},
},
variables: {
input: { id: item.id },
input: { id: item.id, tenantId },
},
}),
];
Expand All @@ -128,10 +155,18 @@ describe('CrudDemoItemListItem: Component', () => {
});

it('should show success message', async () => {
const item = { id: 'test-id', name: 'demo item name' };
const item = { id: 'test-id', name: 'demo item name', tenantId };

const apolloMocks = [
fillCommonQueryWithUser(),
fillCommonQueryWithUser(
currentUserFactory({
tenants: [
tenantFactory({
id: tenantId,
}),
],
})
),
composeMockedQueryResult(crudDemoItemListItemTestQuery, {
data: {
item: {
Expand All @@ -148,7 +183,7 @@ describe('CrudDemoItemListItem: Component', () => {
},
},
variables: {
input: { id: item.id },
input: { id: item.id, tenantId },
},
}),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { currentUserFactory, fillCommonQueryWithUser } from '@sb/webapp-api-client/tests/factories';
import { composeMockedQueryResult } from '@sb/webapp-api-client/tests/utils';
import { trackEvent } from '@sb/webapp-core/services/analytics';
import { getLocalePath } from '@sb/webapp-core/utils';
import { tenantFactory } from '@sb/webapp-tenants/tests/factories/tenant';
import { screen } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { Route, Routes } from 'react-router';
Expand All @@ -13,6 +15,8 @@ import { editCrudDemoItemMutation } from '../editCrudDemoItem.graphql';

jest.mock('@sb/webapp-core/services/analytics');

const tenantId = 'tenantId';

describe('EditCrudDemoItem: Component', () => {
const defaultItemId = 'test-id';
const oldName = 'old item';
Expand All @@ -26,7 +30,6 @@ describe('EditCrudDemoItem: Component', () => {
};
const queryVariables = { id: defaultItemId };

const mutationVariables = { input: { id: defaultItemId, name: newName } };
const mutationData = {
updateCrudDemoItem: { crudDemoItem: { id: defaultItemId, name: newName, __typename: 'CrudDemoItemType' } },
};
Expand Down Expand Up @@ -55,11 +58,20 @@ describe('EditCrudDemoItem: Component', () => {

describe('action completes successfully', () => {
it('should commit mutation', async () => {
const commonQueryMock = fillCommonQueryWithUser(
currentUserFactory({
tenants: [
tenantFactory({
id: tenantId,
}),
],
})
);
const routerProps = createMockRouterProps(RoutesConfig.crudDemoItem.edit, { id: defaultItemId });

const queryMock = fillEditCrudDemoItemQuery(queryData, queryVariables);
const requestMock = composeMockedQueryResult(editCrudDemoItemMutation, {
variables: mutationVariables,
variables: { input: { id: defaultItemId, name: newName, tenantId } },
data: mutationData,
});

Expand All @@ -69,7 +81,7 @@ describe('EditCrudDemoItem: Component', () => {

render(<Component />, {
routerProps,
apolloMocks: (defaultMocks) => defaultMocks.concat(queryMock, requestMock),
apolloMocks: [commonQueryMock, queryMock, requestMock],
});

const nameField = await screen.findByPlaceholderText(/name/i);
Expand All @@ -81,16 +93,25 @@ describe('EditCrudDemoItem: Component', () => {
});

it('should show success message', async () => {
const commonQueryMock = fillCommonQueryWithUser(
currentUserFactory({
tenants: [
tenantFactory({
id: tenantId,
}),
],
})
);
const routerProps = createMockRouterProps(RoutesConfig.crudDemoItem.edit, { id: defaultItemId });
const queryMock = fillEditCrudDemoItemQuery(queryData, queryVariables);
const requestMock = composeMockedQueryResult(editCrudDemoItemMutation, {
variables: mutationVariables,
variables: { input: { id: defaultItemId, name: newName, tenantId } },
data: mutationData,
});

render(<Component />, {
routerProps,
apolloMocks: (defaultMocks) => defaultMocks.concat(queryMock, requestMock),
apolloMocks: [commonQueryMock, queryMock, requestMock],
});

const nameField = await screen.findByPlaceholderText(/name/i);
Expand Down

0 comments on commit 24989b2

Please sign in to comment.