Skip to content

Commit

Permalink
tests for profile module, fix screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
veronikaa-kuznetsova committed Jun 17, 2024
1 parent 5aa933b commit 5a52084
Show file tree
Hide file tree
Showing 66 changed files with 589 additions and 29 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
globals: {
__IS_DEV__: true,
__API__: true,
__PROJECT__: true,
},
overrides: [
{
Expand Down
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.
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_ProfilePage_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_ProfilePage_Normal.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_Primary.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_widget_PageError_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_widget_PageError_Light.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.
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_laptop_pages_ProfilePage_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_laptop_pages_ProfilePage_Normal.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_laptop_shared_Button_Primary.png
Binary file modified .loki/reference/chrome_laptop_widget_PageError_Dark.png
Binary file modified .loki/reference/chrome_laptop_widget_PageError_Light.png
5 changes: 4 additions & 1 deletion config/build/buildPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import { BuildOptions } from './types/config';

export function buildPlugins({ paths, isDev, apiUrl }: BuildOptions): webpack.WebpackPluginInstance[] {
export function buildPlugins({
paths, isDev, apiUrl, project,
}: BuildOptions): webpack.WebpackPluginInstance[] {
const plugins = [
new HtmlWebpackPlugin({
template: paths.html,
Expand All @@ -17,6 +19,7 @@ export function buildPlugins({ paths, isDev, apiUrl }: BuildOptions): webpack.We
new webpack.DefinePlugin({
__IS_DEV__: JSON.stringify(isDev),
__API__: JSON.stringify(apiUrl),
__PROJECT__: JSON.stringify(project),
}),
];

Expand Down
1 change: 1 addition & 0 deletions config/build/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface BuildOptions {
isDev: boolean;
port: number;
apiUrl: string;
project: 'storybook' | 'frontend' | 'jest';
}
1 change: 1 addition & 0 deletions config/jest/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default {
globals: {
__IS_DEV__: true,
__API__: '',
__PROJECT__: 'jest',
},
clearMocks: true,
testEnvironment: 'jsdom',
Expand Down
1 change: 1 addition & 0 deletions config/storybook/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default ({ config }: {config: webpack.Configuration}) => {
config!.plugins!.push(new DefinePlugin({
__IS_DEV__: JSON.stringify(true),
__API__: JSON.stringify(''),
__PROJECT__: JSON.stringify('storybook'),
}));

return config;
Expand Down
4 changes: 2 additions & 2 deletions json-server/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"profile": {
"first": "Вероника",
"lastname": "Кузнецова",
"age": "25",
"age": 25,
"currency": "EUR",
"country": "Belarus",
"country": "Russia",
"city": "Barnaul",
"username": "admin",
"avatar": "https://i.pinimg.com/280x280_RS/ea/9b/b6/ea9bb6e8908b1cd3389733b0503cc8ad.jpg"
Expand Down
7 changes: 6 additions & 1 deletion public/locales/en/profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"username": "Username",
"avatar": "Avatar",
"error_loading_profile": "Error when loading a profile",
"try_reloading_page": "Try reloading the page"
"try_reloading_page": "Try reloading the page",
"first_and_last_name_required": "First and last name required",
"incorrect_age": "Incorrect age",
"specify_country": "Specify country",
"no_data_specified": "No data specified",
"server_error": "Server error"
}
7 changes: 6 additions & 1 deletion public/locales/ru/profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"username": "Имя пользователя",
"avatar": "Аватар",
"error_loading_profile": "Ошибка при загрузке профиля",
"try_reloading_page": "Попробуйте перезагрузить страницу"
"try_reloading_page": "Попробуйте перезагрузить страницу",
"first_and_last_name_required": "Имя и фамилия обязательны",
"incorrect_age": "Некорректный возраст",
"specify_country": "Укажите страну",
"no_data_specified": "Данные не указаны",
"server_error": "Ошибка сервера"
}
1 change: 1 addition & 0 deletions src/app/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare module '*.svg' {

declare const __IS_DEV__: boolean;
declare const __API__: string;
declare const __PROJECT__: 'storybook' | 'frontend' | 'jest';

type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
Expand Down
2 changes: 2 additions & 0 deletions src/entities/Profile/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {
Profile,
ProfileSchema,
ValidateProfileError,
} from './model/types/profile';

export {
Expand All @@ -25,3 +26,4 @@ export { getProfileIsLoading } from './model/selectors/getProfileIsLoading/getPr
export { getProfileError } from './model/selectors/getProfileError/getProfileError';
export { getProfileReadonly } from './model/selectors/getProfileReadonly/getProfileReadonly';
export { getProfileForm } from './model/selectors/getProfileForm/getProfileForm';
export { getProfileValidateErrors } from './model/selectors/getProfileValidateErrors/getProfileValidateErrors';
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 return error', () => {
const data = {
first: 'Veronika',
lastname: 'Kuznetsova',
age: 25,
currency: Currency.USD,
country: Country.Russia,
city: 'Barnaul',
username: 'admin',
};
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 return 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,28 @@
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 return error', () => {
const data = {
first: 'Veronika',
lastname: 'Kuznetsova',
age: 25,
currency: Currency.USD,
country: Country.Russia,
city: 'Barnaul',
username: 'admin',
};
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 work with filled state', () => {
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('getProfileReadonly.test', () => {
test('should work with filled state', () => {
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 'entities/Profile';
import { getProfileValidateErrors } from './getProfileValidateErrors';

describe('getProfileValidateErrors.test', () => {
test('should work with filled state', () => {
const state: DeepPartial<StateSchema> = {
profile: {
validateErrors: [
ValidateProfileError.SERVER_ERROR,
ValidateProfileError.INCORRECT_AGE,
],
},
};
expect(getProfileValidateErrors(state as StateSchema)).toEqual([
ValidateProfileError.SERVER_ERROR,
ValidateProfileError.INCORRECT_AGE,
]);
});
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,3 @@
import { StateSchema } from 'app/providers/StoreProvider';

export const getProfileValidateErrors = (state: StateSchema) => state.profile?.validateErrors;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TestAsyncThunk } from 'shared/lib/tests/TestAsyncThunk/TestAsyncThunk';
import { Country } from 'entities/Country';
import { Currency } from 'entities/Currency';
import { fetchProfileData } from './fetchProfileData';

const data = {
first: 'Veronika',
lastname: 'Kuznetsova',
age: 25,
currency: Currency.USD,
country: Country.Russia,
city: 'Barnaul',
username: 'admin',
};

describe('fetchProfileData.test', () => {
test('success', 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('error login', async () => {
const thunk = new TestAsyncThunk(fetchProfileData);
thunk.api.get.mockReturnValue(Promise.resolve({ status: 403 }));
const result = await thunk.callThunk();

expect(result.meta.requestStatus).toBe('rejected');
});
});
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 = {
first: 'Veronika',
lastname: 'Kuznetsova',
age: 25,
currency: Currency.USD,
country: Country.Russia,
city: 'Barnaul',
username: 'admin',
};

describe('updateProfileData.test', () => {
test('success', 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('error', 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, first: '' },
},
});
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
@@ -1,26 +1,35 @@
import { createAsyncThunk } from '@reduxjs/toolkit';
import { ThunkConfig } from 'app/providers/StoreProvider';
import { Profile } from '../../types/profile';
import { Profile, ValidateProfileError } from '../../types/profile';
import { getProfileForm } from '../../selectors/getProfileForm/getProfileForm';
import { validateProfileData } from '../validateProfileData/validateProfileData';

export const updateProfileData = createAsyncThunk<
Profile,
void,
ThunkConfig<string>
ThunkConfig<ValidateProfileError[]>
>(
'profile/updateProfileData',
async (_, thunkApi) => {
const { extra, rejectWithValue, getState } = thunkApi;

const formData = getProfileForm(getState());
const errors = validateProfileData(formData);
if (errors.length) {
return rejectWithValue(errors);
}

try {
const response = await extra.api.put<Profile>('/profile', formData);

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

return response.data;
} catch (e) {
console.log(e);
return rejectWithValue('error');
return rejectWithValue([ValidateProfileError.SERVER_ERROR]);
}
},
);
Loading

0 comments on commit 5a52084

Please sign in to comment.