Skip to content

Commit

Permalink
chore: add test for saveSession
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Jun 5, 2024
1 parent 304ee0a commit b43e42b
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/GoTrueClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON,
} from './lib/clients'
import { mockUserCredentials } from './lib/utils'
import { Session } from '../src'

describe('GoTrueClient', () => {
// @ts-expect-error 'Allow access to private _refreshAccessToken'
Expand Down Expand Up @@ -1016,4 +1017,52 @@ describe('GoTrueClient with storageisServer = true', () => {
expect(sessionUser).not.toBeNull()
expect(warnings.length).toEqual(0)
})

test('saveSession should overwrite the existing session', async () => {
const store = memoryLocalStorageAdapter()
const client = new GoTrueClient({
url: GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON,
storageKey: 'test-storage-key',
autoRefreshToken: false,
persistSession: true,
storage: {
...store,
},
})
const initialSession: Session = {
access_token: 'test-access-token',
refresh_token: 'test-refresh-token',
expires_in: 3600,
token_type: 'bearer',
user: {
id: 'test-user-id',
aud: 'test-audience',
user_metadata: {},
app_metadata: {},
created_at: new Date().toISOString(),
},
}

// @ts-ignore 'Allow access to private _saveSession'
await client._saveSession(initialSession)
expect(store.getItem('test-storage-key')).toEqual(JSON.stringify(initialSession))

const newSession: Session = {
access_token: 'test-new-access-token',
refresh_token: 'test-new-refresh-token',
expires_in: 3600,
token_type: 'bearer',
user: {
id: 'test-user-id',
aud: 'test-audience',
user_metadata: {},
app_metadata: {},
created_at: new Date().toISOString(),
},
}

// @ts-ignore 'Allow access to private _saveSession'
await client._saveSession(newSession)
expect(store.getItem('test-storage-key')).toEqual(JSON.stringify(newSession))
})
})

0 comments on commit b43e42b

Please sign in to comment.