-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48cb614
commit 813f2d3
Showing
27 changed files
with
308 additions
and
14 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/entities/User/model/selectors/getUserAuthData/getUserAuthData.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { User, UserState } from '../../types/user'; | ||
import { getUserAuthData } from './getUserAuthData'; | ||
|
||
describe('getUserAuthData.test', () => { | ||
test('should return initial state', () => { | ||
const authData: User = { | ||
id: 1, | ||
email: 'email@email.com', | ||
username: 'MarkMelior', | ||
}; | ||
|
||
const initialState: UserState = { authData }; | ||
|
||
const state: Partial<RootState> = { | ||
user: initialState, | ||
}; | ||
|
||
expect(getUserAuthData(state as RootState)).toEqual(initialState); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getUserAuthData(state as RootState)).toEqual(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export interface User { | ||
id: string; | ||
id: number; | ||
email: string; | ||
username?: string; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/features/Auth/model/selector/getLoginState/getLoginState.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { loginInitialState } from '../../slice/loginSlice'; | ||
import { LoginState } from '../../types/loginState'; | ||
import { getLoginState } from './getLoginState'; | ||
|
||
describe('getLoginState.test', () => { | ||
test('should return initial state', () => { | ||
const initialState: LoginState = { | ||
username: 'MarkMelior', | ||
password: 'pass', | ||
isLoading: true, | ||
}; | ||
|
||
const state: Partial<RootState> = { | ||
loginForm: initialState, | ||
}; | ||
|
||
expect(getLoginState(state as RootState)).toEqual(initialState); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getLoginState(state as RootState)).toEqual(loginInitialState); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
...ures/Auth/model/selector/getLoginState.ts → ...l/selector/getLoginState/getLoginState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { loginInitialState } from '../slice/loginSlice'; | ||
import { loginInitialState } from '../../slice/loginSlice'; | ||
|
||
export const getLoginState = (state: RootState) => | ||
state?.loginForm ?? loginInitialState; |
48 changes: 48 additions & 0 deletions
48
src/features/Auth/model/service/loginByUsername/loginByUsername.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { User, userActions } from '@/entities/User'; | ||
import { TestAsyncThunk } from '@/shared/lib/tests'; | ||
import axios from 'axios'; | ||
import { loginByUsername } from './loginByUsername'; | ||
|
||
jest.mock('axios'); | ||
const mockedAxios = jest.mocked(axios); | ||
|
||
describe('loginByUsername.test', () => { | ||
test('success login', async () => { | ||
const userValue: User = { | ||
id: 1, | ||
username: 'MarkMelior', | ||
email: 'email@email.com', | ||
}; | ||
|
||
mockedAxios.post.mockReturnValue(Promise.resolve({ data: userValue })); | ||
|
||
const thunk = new TestAsyncThunk(loginByUsername); | ||
const result = await thunk.callThunk({ | ||
password: 'pass', | ||
username: 'MarkMelior', | ||
}); | ||
|
||
expect(thunk.dispatch).toHaveBeenCalledWith( | ||
userActions.setAuthData(userValue), | ||
); | ||
expect(thunk.dispatch).toHaveBeenCalledTimes(3); | ||
expect(mockedAxios.post).toHaveBeenCalled(); | ||
expect(result.meta.requestStatus).toBe('fulfilled'); | ||
expect(result.payload).toEqual(userValue); | ||
}); | ||
|
||
test('error login', async () => { | ||
mockedAxios.post.mockReturnValue(Promise.resolve({ status: 403 })); | ||
|
||
const thunk = new TestAsyncThunk(loginByUsername); | ||
const result = await thunk.callThunk({ | ||
password: 'pass', | ||
username: 'MarkMelior', | ||
}); | ||
|
||
expect(thunk.dispatch).toHaveBeenCalledTimes(2); | ||
expect(mockedAxios.post).toHaveBeenCalled(); | ||
expect(result.meta.requestStatus).toBe('rejected'); | ||
expect(result.payload).toBe('error'); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { LoginState } from '../types/loginState'; | ||
import { loginActions, loginReducer } from './loginSlice'; | ||
|
||
describe('loginSlice.test', () => { | ||
test('test set username', () => { | ||
const state: Partial<LoginState> = { username: 'Foren' }; | ||
expect( | ||
loginReducer(state as LoginState, loginActions.setUsername('MarkMelior')), | ||
).toEqual({ username: 'MarkMelior' }); | ||
}); | ||
|
||
test('test set password', () => { | ||
const state: Partial<LoginState> = { password: '123' }; | ||
expect( | ||
loginReducer(state as LoginState, loginActions.setPassword('pass')), | ||
).toEqual({ password: 'pass' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/features/Search/model/selector/getQuery/getQuery.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { searchInitialState } from '../../slice/searchSlice'; | ||
import { SearchState } from '../../types/SearchState'; | ||
import { getQuery } from './getQuery'; | ||
|
||
describe('getQuery.test', () => { | ||
test('should return initial state', () => { | ||
const initialState: SearchState = { | ||
query: 'gift', | ||
}; | ||
|
||
const state: Partial<RootState> = { | ||
search: initialState, | ||
}; | ||
|
||
expect(getQuery(state as RootState)).toEqual(initialState); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getQuery(state as RootState)).toEqual(searchInitialState.query); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
...eatures/Search/model/selector/getQuery.ts → ...earch/model/selector/getQuery/getQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { searchInitialState } from '../slice/searchSlice'; | ||
import { searchInitialState } from '../../slice/searchSlice'; | ||
|
||
export const getQuery = (state: RootState) => | ||
state.search?.query ?? searchInitialState.query; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/features/Settings/model/selector/getSettings/getSettings.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { SettingsState } from '../../types/settingsState'; | ||
import { getSettings } from './getSettings'; | ||
|
||
describe('getSettings.test', () => { | ||
test('should return initial state', () => { | ||
const initialState: SettingsState = { | ||
space: false, | ||
optimization: false, | ||
currency: 'RUB', | ||
}; | ||
|
||
const state: Partial<RootState> = { | ||
settings: initialState, | ||
}; | ||
|
||
expect(getSettings(state as RootState)).toEqual(initialState); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getSettings(state as RootState)).toEqual(undefined); | ||
}); | ||
}); |
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
src/features/Settings/model/selector/getSettingsCurrency/getSettingsCurrency.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { SettingsState } from '../../types/settingsState'; | ||
import { getSettingsCurrency } from './getSettingsCurrency'; | ||
|
||
describe('getSettingsCurrency.test', () => { | ||
test('should return initial state', () => { | ||
const initialState: Partial<SettingsState> = { | ||
currency: 'RUB', | ||
}; | ||
|
||
const state: Partial<RootState> = { | ||
settings: initialState as SettingsState, | ||
}; | ||
|
||
expect(getSettingsCurrency(state as RootState)).toEqual('RUB'); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getSettingsCurrency(state as RootState)).toEqual(undefined); | ||
}); | ||
}); |
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
src/features/Settings/model/selector/getSettingsOptimization/getSettingsOptimization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { SettingsState } from '../../types/settingsState'; | ||
import { getSettingsOptimization } from './getSettingsOptimization'; | ||
|
||
describe('getSettingsOptimization.test', () => { | ||
test('should return initial state', () => { | ||
const initialState: Partial<SettingsState> = { | ||
optimization: true, | ||
}; | ||
|
||
const state: Partial<RootState> = { | ||
settings: initialState as SettingsState, | ||
}; | ||
|
||
expect(getSettingsOptimization(state as RootState)).toEqual(true); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getSettingsOptimization(state as RootState)).toEqual(undefined); | ||
}); | ||
}); |
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
src/features/Settings/model/selector/getSettingsSpace/getSettingsSpace.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { SettingsState } from '../../types/settingsState'; | ||
import { getSettingsSpace } from './getSettingsSpace'; | ||
|
||
describe('getSettingsSpace.test', () => { | ||
test('should return initial state', () => { | ||
const initialState: Partial<SettingsState> = { | ||
space: false, | ||
}; | ||
|
||
const state: Partial<RootState> = { | ||
settings: initialState as SettingsState, | ||
}; | ||
|
||
expect(getSettingsSpace(state as RootState)).toEqual(false); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getSettingsSpace(state as RootState)).toEqual(undefined); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { initialState as sortInitialState } from '../../slice/sortSlice'; | ||
import { SortState } from '../../types/sortType'; | ||
import { getSort } from './getSort'; | ||
|
||
describe('getSort.test', () => { | ||
test('should return initial state', () => { | ||
const initialState: SortState = { | ||
category: ['joke'], | ||
sex: ['male'], | ||
age: ['adult'], | ||
sorting: 'popular', | ||
minPrice: 0, | ||
maxPrice: 14000, | ||
}; | ||
|
||
const state: Partial<RootState> = { | ||
sort: initialState, | ||
}; | ||
|
||
expect(getSort(state as RootState)).toEqual(initialState); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: Partial<RootState> = {}; | ||
|
||
expect(getSort(state as RootState)).toEqual(sortInitialState); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
src/features/Sorts/model/selector/getSort.ts → ...s/Sorts/model/selector/getSort/getSort.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import { initialState } from '../slice/sortSlice'; | ||
import { initialState } from '../../slice/sortSlice'; | ||
|
||
export const getSort = (state: RootState) => state.sort ?? initialState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { RootState } from '@/app/providers/StoreProvider'; | ||
import type { AsyncThunkAction } from '@reduxjs/toolkit'; | ||
|
||
type ActionCreatorType<Return, Arg, RejectedValue> = ( | ||
arg: Arg, | ||
) => AsyncThunkAction<Return, Arg, { rejectValue: RejectedValue }>; | ||
|
||
export class TestAsyncThunk<Return, Arg, RejectedValue> { | ||
getState: () => RootState; | ||
dispatch: jest.MockedFn<any>; | ||
actionCreator: ActionCreatorType<Return, Arg, RejectedValue>; | ||
|
||
constructor(actionCreator: ActionCreatorType<Return, Arg, RejectedValue>) { | ||
this.actionCreator = actionCreator; | ||
this.dispatch = jest.fn(); | ||
this.getState = jest.fn(); | ||
} | ||
|
||
async callThunk(arg: Arg) { | ||
const action = this.actionCreator(arg); | ||
const result = await action(this.dispatch, this.getState, undefined); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { TestAsyncThunk } from './TestAsyncThunk/TestAsyncThunk'; | ||
|
||
export { TestAsyncThunk }; |