Skip to content

Commit

Permalink
fix: improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
just-toby committed Oct 11, 2023
1 parent e4faf67 commit 587c91c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/constants/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'

// TODO(WEB-1984): Convert the deadline to minutes and remove unecessary conversions from
// seconds to minutes in the codebase.
// 30 minutes, denominated in seconds
// 10 minutes, denominated in seconds
export const DEFAULT_DEADLINE_FROM_NOW = 60 * 10
export const L2_DEADLINE_FROM_NOW = 60 * 5

Expand Down
67 changes: 51 additions & 16 deletions src/state/migrations/1.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMigrate } from 'redux-persist'
import { RouterPreference } from 'state/routing/types'
import { UserState } from 'state/user/reducer'
import { SlippageTolerance } from 'state/user/types'

import { migration1, PersistAppStateV1 } from './1'
Expand All @@ -24,27 +24,62 @@ const previousState: PersistAppStateV1 = {
}

describe('migration to v1', () => {
it('should migrate the default deadline', () => {
const result = migration1(previousState)
it('should migrate the default deadline', async () => {
const migrator = createMigrate(
{
1: migration1,
},
{ debug: false }
)
const result: any = await migrator(previousState, 1)
expect(result?.user?.userDeadline).toEqual(600)
expect(result?._persist.version).toEqual(1)

expect(result?.user?.userLocale).toEqual(null)
expect(result?.user?.userRouterPreference).toEqual(RouterPreference.API)
expect(result?.user?.userHideClosedPositions).toEqual(false)
expect(result?.user?.userSlippageTolerance).toEqual(SlippageTolerance.Auto)
expect(result?.user?.userSlippageToleranceHasBeenMigratedToAuto).toEqual(true)
expect(result?.user?.tokens).toEqual({})
expect(result?.user?.pairs).toEqual({})
expect(result?.user?.timestamp).toEqual(previousState.user?.timestamp)
expect(result?.user?.hideBaseWalletBanner).toEqual(false)
})

it('should not migrate a non-default value', () => {
const result = migration1({
...previousState,
user: {
...previousState.user,
userDeadline: 300,
} as UserState,
})
it('should not migrate a non-default value', async () => {
const migrator = createMigrate(
{
1: migration1,
},
{ debug: false }
)
const result: any = await migrator(
{
...previousState,
user: {
...previousState.user,
userDeadline: 300,
},
} as PersistAppStateV1,
1
)
expect(result?.user?.userDeadline).toEqual(300)
})

it('should not migrate if user state is not set', () => {
const result = migration1({
...previousState,
user: undefined,
})
it('should not migrate if user state is not set', async () => {
const migrator = createMigrate(
{
1: migration1,
},
{ debug: false }
)
const result: any = await migrator(
{
...previousState,
user: undefined,
} as PersistAppStateV1,
1
)
expect(result?.user?.userDeadline).toBeUndefined()
})
})
4 changes: 4 additions & 0 deletions src/state/migrations/1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const migration1 = (state: PersistAppStateV1 | undefined) => {
...state.user,
userDeadline: DEFAULT_DEADLINE_FROM_NOW,
},
_persist: {
...state._persist,
version: 1,
},
}
}
return state
Expand Down

0 comments on commit 587c91c

Please sign in to comment.