-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for saveUserCsr saga
- Loading branch information
Showing
2 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.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,125 @@ | ||
import { combineReducers } from '@reduxjs/toolkit' | ||
import { expectSaga } from 'redux-saga-test-plan' | ||
import { type Socket } from '../../../types' | ||
import { communitiesReducer, CommunitiesState, type communitiesActions } from '../../communities/communities.slice' | ||
import { StoreKeys } from '../../store.keys' | ||
import { identityAdapter } from '../identity.adapter' | ||
import { identityReducer, IdentityState, type identityActions } from '../identity.slice' | ||
import { saveUserCsrSaga } from './saveUserCsr.saga' | ||
import { type Store } from '../../store.types' | ||
import { type FactoryGirl } from 'factory-girl' | ||
import { setupCrypto } from '@quiet/identity' | ||
import { prepareStore } from '../../../utils/tests/prepareStore' | ||
import { getFactory } from '../../../utils/tests/factories' | ||
import { SocketActionTypes } from '@quiet/types' | ||
import { usersReducer, UsersState } from '../../users/users.slice' | ||
import { certificatesAdapter } from '../../users/users.adapter' | ||
|
||
describe('saveUserCsr', () => { | ||
let store: Store | ||
let factory: FactoryGirl | ||
|
||
beforeEach(async () => { | ||
setupCrypto() | ||
store = prepareStore().store | ||
factory = await getFactory(store) | ||
}) | ||
|
||
test('saves user csr if user csr is absent in csrs list', async () => { | ||
const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket | ||
|
||
const community = | ||
await factory.create<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community') | ||
|
||
const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['payload']>('Identity', { | ||
id: community.id, | ||
nickname: 'john', | ||
}) | ||
|
||
await expectSaga(saveUserCsrSaga, socket) | ||
.withReducer( | ||
combineReducers({ | ||
[StoreKeys.Communities]: communitiesReducer, | ||
[StoreKeys.Identity]: identityReducer, | ||
[StoreKeys.Users]: usersReducer | ||
}), | ||
{ | ||
[StoreKeys.Communities]: { | ||
...new CommunitiesState(), | ||
currentCommunity: community.id, | ||
communities: { | ||
ids: [community.id], | ||
entities: { | ||
[community.id]: community, | ||
}, | ||
}, | ||
}, | ||
[StoreKeys.Identity]: { | ||
...new IdentityState(), | ||
identities: identityAdapter.setAll(identityAdapter.getInitialState(), [identity]), | ||
}, | ||
[StoreKeys.Users]: { | ||
...new UsersState() | ||
} | ||
} | ||
) | ||
.apply(socket, socket.emit, [ | ||
SocketActionTypes.SAVE_USER_CSR, | ||
{ | ||
csr: identity.userCsr?.userCsr | ||
}, | ||
]) | ||
.run() | ||
}) | ||
test("do not save user csr if it's already included in csrs list", async () => { | ||
|
||
const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket | ||
|
||
const community = | ||
await factory.create<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community') | ||
|
||
const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['payload']>('Identity', { | ||
id: community.id, | ||
nickname: 'john', | ||
}) | ||
|
||
console.log(identity.userCsr?.userCsr) | ||
|
||
await expectSaga(saveUserCsrSaga, socket) | ||
.withReducer( | ||
combineReducers({ | ||
[StoreKeys.Communities]: communitiesReducer, | ||
[StoreKeys.Identity]: identityReducer, | ||
[StoreKeys.Users]: usersReducer | ||
}), | ||
{ | ||
[StoreKeys.Communities]: { | ||
...new CommunitiesState(), | ||
currentCommunity: community.id, | ||
communities: { | ||
ids: [community.id], | ||
entities: { | ||
[community.id]: community, | ||
}, | ||
}, | ||
}, | ||
[StoreKeys.Identity]: { | ||
...new IdentityState(), | ||
identities: identityAdapter.setAll(identityAdapter.getInitialState(), [identity]), | ||
}, | ||
[StoreKeys.Users]: { | ||
...new UsersState(), | ||
csrs: certificatesAdapter.setAll(certificatesAdapter.getInitialState(), [identity.userCsr?.pkcs10.pkcs10]) | ||
} | ||
} | ||
) | ||
.not | ||
.apply(socket, socket.emit, [ | ||
SocketActionTypes.SAVE_USER_CSR, | ||
{ | ||
csr: identity.userCsr?.userCsr | ||
}, | ||
]) | ||
.run() | ||
}) | ||
}) |
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