Skip to content

Commit

Permalink
feat: add profile slice, thunks, selectors tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomatoVan committed Dec 14, 2023
1 parent 9aa19ff commit 282297e
Show file tree
Hide file tree
Showing 81 changed files with 383 additions and 4 deletions.
Binary file modified .loki/reference/chrome_iphone7_pages_NotFoundPage_Dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_pages_NotFoundPage_Normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_AppLink_Primary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_AppLink_Primary_Dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_AppLink_Red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_AppLink_Red_Dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_AppLink_Secondary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Clear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Outline_Dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Outline_Size_L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Primary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Square_L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Square_M.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Button_Square_XL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Input_Primary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Loader_Dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Loader_Normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .loki/reference/chrome_iphone7_shared_Modal_Dark.png
Binary file modified .loki/reference/chrome_iphone7_shared_Modal_Primary.png
Binary file modified .loki/reference/chrome_iphone7_shared_ThemeSwitcher_Dark.png
Binary file modified .loki/reference/chrome_iphone7_shared_ThemeSwitcher_Normal.png
Binary file modified .loki/reference/chrome_iphone7_widget_ErrorPage_Dark.png
Binary file modified .loki/reference/chrome_iphone7_widget_ErrorPage_Light.png
Diff not rendered.
Diff not rendered.
Binary file modified .loki/reference/chrome_laptop_pages_NotFoundPage_Dark.png
Binary file modified .loki/reference/chrome_laptop_pages_NotFoundPage_Normal.png
Binary file modified .loki/reference/chrome_laptop_shared_AppLink_Primary.png
Binary file modified .loki/reference/chrome_laptop_shared_AppLink_Primary_Dark.png
Binary file modified .loki/reference/chrome_laptop_shared_AppLink_Red.png
Binary file modified .loki/reference/chrome_laptop_shared_AppLink_Red_Dark.png
Binary file modified .loki/reference/chrome_laptop_shared_AppLink_Secondary.png
Binary file modified .loki/reference/chrome_laptop_shared_AppLink_Secondary_Dark.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Clear.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Clear_Inverted.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Disabled.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Outline.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Outline_Dark.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Outline_Size_L.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Outline_Size_XL.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Primary.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Square.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Square_L.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Square_M.png
Binary file modified .loki/reference/chrome_laptop_shared_Button_Square_XL.png
Binary file modified .loki/reference/chrome_laptop_shared_Input_Primary.png
Binary file modified .loki/reference/chrome_laptop_shared_Loader_Dark.png
Binary file modified .loki/reference/chrome_laptop_shared_Loader_Normal.png
Binary file modified .loki/reference/chrome_laptop_shared_Modal_Dark.png
Binary file modified .loki/reference/chrome_laptop_shared_Modal_Primary.png
Binary file modified .loki/reference/chrome_laptop_shared_ThemeSwitcher_Dark.png
Binary file modified .loki/reference/chrome_laptop_shared_ThemeSwitcher_Normal.png
Binary file modified .loki/reference/chrome_laptop_widget_ErrorPage_Dark.png
Binary file modified .loki/reference/chrome_laptop_widget_ErrorPage_Light.png
Diff not rendered.
Diff not rendered.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { StateSchema } from 'app/providers/StoreProvider';
import { Country } from 'entities/Country';
import { Currency } from 'entities/Currency';
import { getProfileData } from './getProfileData';

describe('getProfileData.test', () => {
test('should get profile data', () => {
const data = {
username: 'test',
first: 'test firstname',
lastname: 'test lastname',
age: 20,
country: Country.Armenia,
currency: Currency.EUR,
avatar: 'https://pic.rutubelist.ru/user/3b/27/3b2758ad5492a76b578f7ee072e4e894.jpg',
city: 'yaroslavl',
};
const state: DeepPartial<StateSchema> = {
profile: {
data,
},
};
expect(getProfileData(state as StateSchema)).toEqual(data);
});
test('should work with empty state', () => {
const state: DeepPartial<StateSchema> = {};
expect(getProfileData(state as StateSchema)).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StateSchema } from 'app/providers/StoreProvider';
import { getProfileError } from './getProfileError';

describe('getProfileError.test', () => {
test('should get profile error', () => {
const state: DeepPartial<StateSchema> = {
profile: {
error: '123',
},
};
expect(getProfileError(state as StateSchema)).toEqual('123');
});
test('should work with empty state', () => {
const state: DeepPartial<StateSchema> = {};
expect(getProfileError(state as StateSchema)).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { StateSchema } from 'app/providers/StoreProvider';
import { Country } from 'entities/Country';
import { Currency } from 'entities/Currency';
import { getProfileForm } from './getProfileForm';

describe('getProfileForm.test', () => {
test('should get profile form data', () => {
const data = {
username: 'test',
first: 'test firstname',
lastname: 'test lastname',
age: 20,
country: Country.Armenia,
currency: Currency.EUR,
avatar:
'https://pic.rutubelist.ru/user/3b/27/3b2758ad5492a76b578f7ee072e4e894.jpg',
city: 'yaroslavl',
};
const state: DeepPartial<StateSchema> = {
profile: {
form: data,
},
};
expect(getProfileForm(state as StateSchema)).toEqual(data);
});
test('should work with empty state', () => {
const state: DeepPartial<StateSchema> = {};
expect(getProfileForm(state as StateSchema)).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StateSchema } from 'app/providers/StoreProvider';
import { getProfileIsLoading } from './getProfileIsLoading';

describe('getProfileIsLoading.test', () => {
test('should get profile isLoading', () => {
const state: DeepPartial<StateSchema> = {
profile: {
isLoading: true,
},
};
expect(getProfileIsLoading(state as StateSchema)).toEqual(true);
});
test('should work with empty state', () => {
const state: DeepPartial<StateSchema> = {};
expect(getProfileIsLoading(state as StateSchema)).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StateSchema } from 'app/providers/StoreProvider';
import { getProfileReadonly } from './getProfileReadonly';

describe('getProfileIsLoading.test', () => {
test('should get profile isLoading', () => {
const state: DeepPartial<StateSchema> = {
profile: {
readonly: true,
},
};
expect(getProfileReadonly(state as StateSchema)).toEqual(true);
});
test('should work with empty state', () => {
const state: DeepPartial<StateSchema> = {};
expect(getProfileReadonly(state as StateSchema)).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { StateSchema } from 'app/providers/StoreProvider';
import { ValidateProfileError } from '../../types/profile';
import { getProfileValidateErrors } from './getProfileValidateErrors';

describe('getProfileIsLoading.test', () => {
test('should get profile isLoading', () => {
const state: DeepPartial<StateSchema> = {
profile: {
validateErrors: [
ValidateProfileError.SERVER_ERROR,
ValidateProfileError.INCORRECT_USER_DATA,
],
},
};
expect(getProfileValidateErrors(state as StateSchema)).toEqual([
ValidateProfileError.SERVER_ERROR,
ValidateProfileError.INCORRECT_USER_DATA,
]);
});
test('should work with empty state', () => {
const state: DeepPartial<StateSchema> = {};
expect(getProfileValidateErrors(state as StateSchema)).toEqual(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { userActions } from 'entities/User';
import { TestAsyncThunk } from 'shared/lib/tests/TestAsyncThunk/TestAsyncThunk';
import { Country } from 'entities/Country';
import { Currency } from 'entities/Currency';
import { fetchProfileData } from './fetchProfileData';

const data = {
username: 'test',
first: 'test firstname',
lastname: 'test lastname',
age: 20,
country: Country.Armenia,
currency: Currency.EUR,
avatar: 'https://pic.rutubelist.ru/user/3b/27/3b2758ad5492a76b578f7ee072e4e894.jpg',
city: 'yaroslavl',
};

describe('fetchProfileData.test', () => {
test('fetchProfileData async thunk test', async () => {
const thunk = new TestAsyncThunk(fetchProfileData);

thunk.api.get.mockReturnValue(Promise.resolve({ data }));

const result = await thunk.callThunk();

expect(thunk.api.get).toHaveBeenCalled();
expect(result.meta.requestStatus).toBe('fulfilled');
expect(result.payload).toEqual(data);
});

test('fetchProfileData async error thunk test', async () => {
const thunk = new TestAsyncThunk(fetchProfileData);
thunk.api.get.mockReturnValue(Promise.resolve({ status: 403 }));

const result = await thunk.callThunk();

expect(thunk.api.get).toHaveBeenCalled();
expect(result.meta.requestStatus).toBe('rejected');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ ThunkConfig<string>
try {
const response = await extra.api.get<Profile>('/profile');

if (!response.data) {
throw new Error();
}
return response.data;
} catch (error) {
return rejectWithValue('error');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { TestAsyncThunk } from 'shared/lib/tests/TestAsyncThunk/TestAsyncThunk';
import { Country } from 'entities/Country';
import { Currency } from 'entities/Currency';
import { ValidateProfileError } from 'entities/Profile';
import { updateProfileData } from './updateProfileData';

const data = {
username: 'test',
first: 'test firstname',
lastname: 'test lastname',
age: 20,
country: Country.Armenia,
currency: Currency.EUR,
avatar:
'https://pic.rutubelist.ru/user/3b/27/3b2758ad5492a76b578f7ee072e4e894.jpg',
city: 'yaroslavl',
};

describe('updateProfileData.test', () => {
test('updateProfileData async thunk test', async () => {
const thunk = new TestAsyncThunk(updateProfileData, {
profile: {
form: data,
},
});

thunk.api.put.mockReturnValue(Promise.resolve({ data }));

const result = await thunk.callThunk();

expect(thunk.api.put).toHaveBeenCalled();
expect(result.meta.requestStatus).toBe('fulfilled');
expect(result.payload).toEqual(data);
});

test('updateProfileData async error thunk test', async () => {
const thunk = new TestAsyncThunk(updateProfileData, {
profile: {
form: data,
},
});
thunk.api.put.mockReturnValue(Promise.resolve({ status: 403 }));

const result = await thunk.callThunk();

expect(result.meta.requestStatus).toBe('rejected');
expect(result.payload).toEqual([ValidateProfileError.SERVER_ERROR]);
});

test('validate error', async () => {
const thunk = new TestAsyncThunk(updateProfileData, {
profile: {
form: { ...data, lastname: '' },
},
});
thunk.api.put.mockReturnValue(Promise.resolve({ status: 403 }));

const result = await thunk.callThunk();

expect(result.meta.requestStatus).toBe('rejected');
expect(result.payload).toEqual([ValidateProfileError.INCORRECT_USER_DATA]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const updateProfileData = createAsyncThunk<
try {
const response = await extra.api.put<Profile>('/profile', formData);

if (!response.data) {
throw new Error();
}

return response.data;
} catch (error) {
return rejectWithValue([ValidateProfileError.SERVER_ERROR]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Country } from 'entities/Country';
import { Currency } from 'entities/Currency';
import { validateProfileData } from 'entities/Profile/model/services/validateProfileData/validateProfileData';
import { ValidateProfileError } from 'entities/Profile';

const data = {
username: 'test',
first: 'test firstname',
lastname: 'test lastname',
age: 20,
country: Country.Armenia,
currency: Currency.EUR,
avatar:
'https://pic.rutubelist.ru/user/3b/27/3b2758ad5492a76b578f7ee072e4e894.jpg',
city: 'yaroslavl',
};

describe('validateProfileData.test', () => {
test('validateProfileData async thunk test', async () => {
const result = validateProfileData(data);

expect(result).toEqual([]);
});

test('validateProfileData with errors test (first, lastname)', async () => {
const result = validateProfileData({ ...data, first: '', lastname: '' });

expect(result).toEqual([ValidateProfileError.INCORRECT_USER_DATA]);
});

test('validateProfileData with errors test (age)', async () => {
const result = validateProfileData({ ...data, age: undefined });

expect(result).toEqual([ValidateProfileError.INCORRECT_AGE]);
});

test('validateProfileData with errors test (country)', async () => {
const result = validateProfileData({ ...data, country: undefined });

expect(result).toEqual([ValidateProfileError.INCORRECT_COUNTRY]);
});

test('validateProfileData with errors test (all)', async () => {
const result = validateProfileData({});

expect(result).toEqual([
ValidateProfileError.INCORRECT_USER_DATA,
ValidateProfileError.INCORRECT_AGE,
ValidateProfileError.INCORRECT_COUNTRY,
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Profile, ValidateProfileError } from '../../types/profile';

export const validateProfileData = (profile: Profile | undefined) => {
export const validateProfileData = (profile?: Profile) => {
if (!profile) {
return [ValidateProfileError.NO_DATA];
}
Expand Down
Loading

0 comments on commit 282297e

Please sign in to comment.