Skip to content

Commit

Permalink
add test add env
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Pashin committed Dec 4, 2023
1 parent f8db570 commit 8d62615
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ module.exports = {
globals: {
__IS_DEV__: true,
__API__: true,
__PROJECT__: true,
},
overrides: [
{
Expand Down
1 change: 1 addition & 0 deletions config/build/buildPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const buildPlugins = (options: BuildOptions): webpack.WebpackPluginInstan
new webpack.DefinePlugin({
__IS_DEV__: JSON.stringify(options.isDev),
__API__: JSON.stringify(options.apiUrl),
__PROJECT___: JSON.stringify(options.project),
}),
new ReactRefreshWebpackPlugin(),
];
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 @@ -21,4 +21,5 @@ export interface BuildOptions {
isDev: BuildIsDev,
port: BuildPort,
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 @@ -67,6 +67,7 @@ export default {
globals: {
__IS_DEV__: true,
__API__: '',
__PROJECT__: 'jest',
},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
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
2 changes: 2 additions & 0 deletions src/app/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ declare module '*.svg' {
declare const __IS_DEV__: boolean;
// eslint-disable-next-line
declare const __API__: string;
// eslint-disable-next-line
declare const __PROJECT__: string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { TestAsyncFunc } from 'shared/lib/tests/TestAsyncFunc/TestAsyncFunc';
import { fetchProfileData } from './fetchProfileData';
import { Profile } from '../../types/profile';

jest.mock('axios');

const profileData: Profile = {
age: 99,
city: 'Any city',
country: 'Any country',
email: 'Any email',
firstName: 'Any firstName',
secondName: 'Any secondName',
username: 'username',
};

describe('fetchProfileData.test', () => {
test('success', async () => {
const thunk = new TestAsyncFunc(fetchProfileData);
thunk.api.get.mockReturnValue(Promise.resolve({ data: profileData }));

const result = await thunk.callThunc();

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

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

const result = await thunk.callThunc();

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

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

return response.data;
} catch (e) {
return rejectWithValue('error');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { TestAsyncFunc } from 'shared/lib/tests/TestAsyncFunc/TestAsyncFunc';
import { updateProfileData } from './updateProfileData';
import { Profile, ValidateProfileError } from '../../types/profile';

jest.mock('axios');

const profileData: Profile = {
age: 99,
city: 'Any city',
country: 'Any country',
email: 'Any email',
firstName: 'Any firstName',
secondName: 'Any secondName',
username: 'username',
};

describe('fetchProfileData.test', () => {
test('success', async () => {
const thunk = new TestAsyncFunc(
updateProfileData,
{
profile: {
form: profileData,
},
},
);
thunk.api.put.mockReturnValue(Promise.resolve({ data: profileData }));

const result = await thunk.callThunc();

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

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

const result = await thunk.callThunc();

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

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

const result = await thunk.callThunc();

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

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

return response.data;
} catch (e) {
return rejectWithValue([ValidateProfileError.SERVER_ERROR]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { validateProfileData } from './validateProfileData';
import { Profile, ValidateProfileError } from '../../types/profile';

jest.mock('axios');

const profileData: Profile = {
age: 99,
city: 'Any city',
country: 'Any country',
email: 'Any email',
firstName: 'Any firstName',
secondName: 'Any secondName',
username: 'username',
};

describe('validateProfileData.test', () => {
test('success', async () => {
const result = validateProfileData(profileData);

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

test('without secondname', async () => {
const result = validateProfileData({
...profileData,
secondName: '',
});

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

test('without age', async () => {
const result = validateProfileData({
...profileData,
age: undefined,
});

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

test('with nothing', async () => {
const result = validateProfileData({});
expect(result).toEqual([
ValidateProfileError.INCORRECT_USER_DATA,
ValidateProfileError.INCORRECT_COUNTRY,
ValidateProfileError.INCORRECT_AGE,
]);
});
});
1 change: 1 addition & 0 deletions src/shared/const/isFrontend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isFrontend = __PROJECT__ === 'frontend';
1 change: 1 addition & 0 deletions src/shared/const/isJest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isJest = __PROJECT__ === 'jest';
1 change: 1 addition & 0 deletions src/shared/const/isStorybook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isStorybook = __PROJECT__ === 'storybook';
9 changes: 6 additions & 3 deletions src/shared/lib/tests/TestAsyncFunc/TestAsyncFunc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncThunkAction } from '@reduxjs/toolkit';
import { AsyncThunkAction, DeepPartial } from '@reduxjs/toolkit';
import { StateSchema } from 'app/providers/StoreProvider';
import axios, { AxiosStatic } from 'axios';

Expand All @@ -23,10 +23,13 @@ export class TestAsyncFunc<Return, Arg, RejectedValue> {

navigate: jest.MockedFn<any>;

Check warning on line 24 in src/shared/lib/tests/TestAsyncFunc/TestAsyncFunc.ts

View workflow job for this annotation

GitHub Actions / pipeline (17.x)

Unexpected any. Specify a different type

constructor(actionCreator: ActionCreatorType<Return, Arg, RejectedValue>) {
constructor(
actionCreator: ActionCreatorType<Return, Arg, RejectedValue>,
state?: DeepPartial<StateSchema>,
) {
this.actionCreator = actionCreator;
this.dispatch = jest.fn();
this.getState = jest.fn();
this.getState = jest.fn(() => state as StateSchema);
this.navigate = jest.fn();

this.api = mockedAxios;
Expand Down
1 change: 1 addition & 0 deletions webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default (env: BuildEnv) => {
isDev,
port,
apiUrl,
project: 'frontend',
});

return config;
Expand Down

0 comments on commit 8d62615

Please sign in to comment.