Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman committed Sep 8, 2024
1 parent dfec55c commit b6552f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
23 changes: 12 additions & 11 deletions src/pages/Account/api/__tests__/useUpdateProfile.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterAll, describe, expect, it, vi } from 'vitest';

import { renderHook, waitFor } from 'test/test-utils';
import { userFixture1 } from '__fixtures__/users';
import { profileFixture1 } from '__fixtures__/profiles';
import storage from 'common/utils/storage';
import { StorageKey } from 'common/utils/constants';

Expand All @@ -13,19 +13,19 @@ describe('useUpdateProfile', () => {
});

afterAll(() => {
storage.removeItem(StorageKey.User);
storage.removeItem(StorageKey.UserProfile);
});

it('should update profile', async () => {
// ARRANGE
let isSuccess = false;
storage.setItem(StorageKey.User, JSON.stringify(userFixture1));
storage.setItem(StorageKey.UserProfile, JSON.stringify(profileFixture1));
const { result } = renderHook(() => useUpdateProfile());
await waitFor(() => expect(result.current).not.toBeNull());

// ACT
result.current.mutate(
{ profile: userFixture1 },
{ profile: profileFixture1 },
{
onSuccess: () => {
isSuccess = true;
Expand All @@ -38,17 +38,17 @@ describe('useUpdateProfile', () => {
expect(isSuccess).toBe(true);
});

it('should error when no stored profile', async () => {
it('should create profile when no existing profile', async () => {
// ARRANGE
let isSuccess = false;
let isError = false;
storage.removeItem(StorageKey.User);
storage.removeItem(StorageKey.UserProfile);
const { result } = renderHook(() => useUpdateProfile());
await waitFor(() => expect(result.current).not.toBeNull());

// ACT
result.current.mutate(
{ profile: userFixture1 },
{ profile: profileFixture1 },
{
onSuccess: () => {
isSuccess = true;
Expand All @@ -58,11 +58,12 @@ describe('useUpdateProfile', () => {
},
},
);
await waitFor(() => expect(result.current.isError).toBe(true));
await waitFor(() => expect(result.current.isSuccess).toBe(true));

// ASSERT
expect(isSuccess).toBe(false);
expect(isError).toBe(true);
expect(isSuccess).toBe(true);
expect(isError).toBe(false);
expect(result.current.data).toEqual(profileFixture1);
});

it('should error when an error is caught in mutation function', async () => {
Expand All @@ -78,7 +79,7 @@ describe('useUpdateProfile', () => {

// ACT
result.current.mutate(
{ profile: userFixture1 },
{ profile: profileFixture1 },
{
onSuccess: () => {
isSuccess = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { UseMutationResult } from '@tanstack/react-query';
import { render, screen } from 'test/test-utils';
import { User } from 'common/models/user';
import * as UseUpdateProfile from 'pages/Account/api/useUpdateProfile';
import { userFixture1 } from '__fixtures__/users';
import { profileFixture1 } from '__fixtures__/profiles';

import ProfileForm from '../ProfileForm';

Expand All @@ -27,7 +27,7 @@ describe('ProfileForm', async () => {

it('should render successfully', async () => {
// ARRANGE
render(<ProfileForm user={userFixture1} />);
render(<ProfileForm profile={profileFixture1} />);
await screen.findByTestId('form-profile');

// ASSERT
Expand All @@ -41,21 +41,17 @@ describe('ProfileForm', async () => {
useUpdateProfileSpy.mockReturnValueOnce({
mutate: mockUpdateProfile,
} as unknown as UseMutationResult<User, Error, UseUpdateProfile.UpdateProfileVariables>);
render(<ProfileForm user={userFixture1} testid="form" />);
render(<ProfileForm profile={profileFixture1} testid="form" />);
await screen.findByTestId('form-field-name');

// ACT
await userEvent.click(screen.getByTestId('form-field-name'));
await userEvent.clear(screen.getByLabelText('Name'));
await userEvent.type(screen.getByLabelText('Name'), 'Valid Name');
await userEvent.clear(screen.getByLabelText('Username'));
await userEvent.type(screen.getByLabelText('Username'), 'ValidUsername');
await userEvent.clear(screen.getByLabelText('Email'));
await userEvent.type(screen.getByLabelText('Email'), '[email protected]');
await userEvent.clear(screen.getByLabelText('Phone'));
await userEvent.type(screen.getByLabelText('Phone'), '123-456-7890');
await userEvent.clear(screen.getByLabelText('Website'));
await userEvent.type(screen.getByLabelText('Website'), 'http://valid.example.com');
await userEvent.clear(screen.getByLabelText('Bio'));
await userEvent.type(screen.getByLabelText('Bio'), 'My name is Valid Name.');
await userEvent.click(screen.getByTestId('form-button-submit'));

// ASSERT
Expand All @@ -65,7 +61,7 @@ describe('ProfileForm', async () => {

it('should perform cancel', async () => {
// ARRANGE
render(<ProfileForm user={userFixture1} testid="form" />);
render(<ProfileForm profile={profileFixture1} testid="form" />);
await screen.findByTestId('form');

// ACT
Expand Down

0 comments on commit b6552f7

Please sign in to comment.