Skip to content

Commit

Permalink
test: use user repo instead of axios
Browse files Browse the repository at this point in the history
  • Loading branch information
g-saracca committed Nov 29, 2024
1 parent bddfcac commit 63a7cf9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
4 changes: 4 additions & 0 deletions tests/component/sections/sign-up/SignUp.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { DataverseInfoRepository } from '@/info/domain/repositories/DataverseInfoRepository'
import { SignUp } from '@/sections/sign-up/SignUp'
import { UserRepository } from '@/users/domain/repositories/UserRepository'
import { AuthContextMother } from '@tests/component/auth/AuthContextMother'
import { AuthContext } from 'react-oauth2-code-pkce'

const dataverseInfoRepository: DataverseInfoRepository = {} as DataverseInfoRepository
const userRepository: UserRepository = {} as UserRepository

describe('SignUp', () => {
beforeEach(() => {
Expand All @@ -25,6 +27,7 @@ describe('SignUp', () => {
login: () => {} // 👈 deprecated
}}>
<SignUp
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
hasValidTokenButNotLinkedAccount={true}
/>
Expand Down Expand Up @@ -54,6 +57,7 @@ describe('SignUp', () => {
login: () => {} // 👈 deprecated
}}>
<SignUp
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
hasValidTokenButNotLinkedAccount={false}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
// import { axiosInstance } from '@/axiosInstance'
import { AuthContext } from 'react-oauth2-code-pkce'
import { DataverseInfoRepository } from '@/info/domain/repositories/DataverseInfoRepository'
import { UserRepository } from '@/users/domain/repositories/UserRepository'
import { ValidTokenNotLinkedAccountForm } from '@/sections/sign-up/valid-token-not-linked-account-form/ValidTokenNotLinkedAccountForm'
import { AuthContextMother } from '@tests/component/auth/AuthContextMother'
import { AuthContext } from 'react-oauth2-code-pkce'
import { UserDTO } from '@/users/domain/useCases/DTOs/UserDTO'

const dataverseInfoRepository: DataverseInfoRepository = {} as DataverseInfoRepository
const userRepository: UserRepository = {} as UserRepository

const termsOfUseMock = 'Terms of use'
const mockUserName = 'mockUserName'
const mockFirstName = 'mockFirstName'
const mockLastName = 'mockLastName'
const mockEmail = '[email protected]'

const successfullStaticResponse = {
data: {
status: 'OK',
data: {
message: 'User registered.'
}
},
status: 200,
statusText: 'OK'
}

describe('ValidTokenNotLinkedAccountForm', () => {
beforeEach(() => {
dataverseInfoRepository.getTermsOfUse = cy.stub().resolves(termsOfUseMock)

// Intercept axios register call to avoid real API call
cy.intercept('POST', '/api/users/register', successfullStaticResponse).as('registerUserCall')
userRepository.register = cy.stub().as('registerUser').resolves()
})

describe('form fields correct values', () => {
Expand All @@ -51,7 +40,10 @@ describe('ValidTokenNotLinkedAccountForm', () => {
error: null,
login: () => {} // 👈 deprecated
}}>
<ValidTokenNotLinkedAccountForm dataverseInfoRepository={dataverseInfoRepository} />
<ValidTokenNotLinkedAccountForm
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
/>
</AuthContext.Provider>
)

Expand All @@ -76,7 +68,10 @@ describe('ValidTokenNotLinkedAccountForm', () => {
error: null,
login: () => {} // 👈 deprecated
}}>
<ValidTokenNotLinkedAccountForm dataverseInfoRepository={dataverseInfoRepository} />
<ValidTokenNotLinkedAccountForm
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
/>
</AuthContext.Provider>
)

Expand Down Expand Up @@ -113,7 +108,10 @@ describe('ValidTokenNotLinkedAccountForm', () => {
error: null,
login: () => {} // 👈 deprecated
}}>
<ValidTokenNotLinkedAccountForm dataverseInfoRepository={dataverseInfoRepository} />
<ValidTokenNotLinkedAccountForm
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
/>
</AuthContext.Provider>
)

Expand All @@ -123,10 +121,11 @@ describe('ValidTokenNotLinkedAccountForm', () => {

cy.findByRole('button', { name: 'Create Account' }).click()

cy.wait('@registerUserCall').then((interception) => {
const requestBody = interception.request.body as Record<string, string>
cy.get('@registerUser').should((spy) => {
const registerUserSpy = spy as unknown as Cypress.Agent<sinon.SinonSpy>
const userDTO = registerUserSpy.getCall(0).args[0] as UserDTO

expect(requestBody).to.deep.equal({
expect(userDTO).to.deep.equal({
termsAccepted: true
})
})
Expand All @@ -146,7 +145,10 @@ describe('ValidTokenNotLinkedAccountForm', () => {
error: null,
login: () => {} // 👈 deprecated
}}>
<ValidTokenNotLinkedAccountForm dataverseInfoRepository={dataverseInfoRepository} />
<ValidTokenNotLinkedAccountForm
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
/>
</AuthContext.Provider>
)

Expand Down Expand Up @@ -198,10 +200,11 @@ describe('ValidTokenNotLinkedAccountForm', () => {

cy.findByRole('button', { name: 'Create Account' }).click()

cy.wait('@registerUserCall').then((interception) => {
const requestBody = interception.request.body as Record<string, string>
cy.get('@registerUser').should((spy) => {
const registerUserSpy = spy as unknown as Cypress.Agent<sinon.SinonSpy>
const userDTO = registerUserSpy.getCall(0).args[0] as UserDTO

expect(requestBody).to.deep.equal({
expect(userDTO).to.deep.equal({
termsAccepted: true,
username: newMockUserName,
emailAddress: newMockEmail,
Expand All @@ -228,7 +231,10 @@ describe('ValidTokenNotLinkedAccountForm', () => {
error: null,
login: () => {} // 👈 deprecated
}}>
<ValidTokenNotLinkedAccountForm dataverseInfoRepository={dataverseInfoRepository} />
<ValidTokenNotLinkedAccountForm
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
/>
</AuthContext.Provider>
)

Expand All @@ -251,7 +257,10 @@ describe('ValidTokenNotLinkedAccountForm', () => {
error: null,
login: () => {} // 👈 deprecated
}}>
<ValidTokenNotLinkedAccountForm dataverseInfoRepository={dataverseInfoRepository} />
<ValidTokenNotLinkedAccountForm
userRepository={userRepository}
dataverseInfoRepository={dataverseInfoRepository}
/>
</AuthContext.Provider>
)

Expand Down

0 comments on commit 63a7cf9

Please sign in to comment.