Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions packages/core/src/auth/authStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('authStore', () => {
},
})

const {options, dashboardContext} = authStore.getInitialState(instance)
const {options, dashboardContext} = authStore.getInitialState(instance, null)

expect(options.apiHost).toBe(apiHost)
expect(options.callbackUrl).toBe(callbackUrl)
Expand All @@ -114,7 +114,7 @@ describe('authStore', () => {
auth: {initialLocationHref},
})

const {dashboardContext, authState} = authStore.getInitialState(instance)
const {dashboardContext, authState} = authStore.getInitialState(instance, null)
expect(dashboardContext).toEqual(context)
expect(authState.type).toBe(AuthStateType.LOGGED_OUT)
})
Expand All @@ -129,7 +129,7 @@ describe('authStore', () => {
auth: {initialLocationHref},
})

const {dashboardContext, authState} = authStore.getInitialState(instance)
const {dashboardContext, authState} = authStore.getInitialState(instance, null)
expect(dashboardContext).toEqual(expectedContext)
expect(authState.type).toBe(AuthStateType.LOGGED_OUT)
})
Expand All @@ -143,7 +143,7 @@ describe('authStore', () => {
auth: {initialLocationHref},
})

const {dashboardContext, authState} = authStore.getInitialState(instance)
const {dashboardContext, authState} = authStore.getInitialState(instance, null)
expect(dashboardContext).toStrictEqual({})
expect(authState.type).toBe(AuthStateType.LOGGED_OUT)
expect(errorSpy).toHaveBeenCalledWith(
Expand All @@ -166,7 +166,7 @@ describe('authStore', () => {
},
})

const {authState, dashboardContext} = authStore.getInitialState(instance)
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
expect(authState).toMatchObject({type: AuthStateType.LOGGED_IN, token})
expect(dashboardContext).toEqual(context)
})
Expand All @@ -182,7 +182,7 @@ describe('authStore', () => {

vi.mocked(getAuthCode).mockReturnValue('auth-code')

const {authState, dashboardContext} = authStore.getInitialState(instance)
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
expect(authState).toMatchObject({type: AuthStateType.LOGGING_IN})
expect(dashboardContext).toEqual(context)
})
Expand All @@ -197,7 +197,7 @@ describe('authStore', () => {
vi.mocked(getAuthCode).mockReturnValue(null)
vi.mocked(getTokenFromStorage).mockReturnValue(storageToken)

const {authState, dashboardContext} = authStore.getInitialState(instance)
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
expect(authState).toMatchObject({type: AuthStateType.LOGGED_IN, token: storageToken})
expect(dashboardContext).toStrictEqual({})
})
Expand All @@ -215,7 +215,7 @@ describe('authStore', () => {
vi.mocked(getAuthCode).mockReturnValue(null)
vi.mocked(getTokenFromStorage).mockReturnValue(storageToken)

const {authState, dashboardContext} = authStore.getInitialState(instance)
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
expect(authState).toMatchObject({type: AuthStateType.LOGGED_OUT})
expect(dashboardContext).toEqual(context)
})
Expand All @@ -229,7 +229,7 @@ describe('authStore', () => {
vi.mocked(getAuthCode).mockReturnValue(null)
vi.mocked(getTokenFromStorage).mockReturnValue(null)

const {authState, dashboardContext} = authStore.getInitialState(instance)
const {authState, dashboardContext} = authStore.getInitialState(instance, null)
expect(authState).toMatchObject({type: AuthStateType.LOGGED_OUT})
expect(dashboardContext).toStrictEqual({})
})
Expand All @@ -251,7 +251,7 @@ describe('authStore', () => {
auth: {storageArea: mockStorage}, // Provide mock storage
})

const {authState, options} = authStore.getInitialState(instance)
const {authState, options} = authStore.getInitialState(instance, null)
expect(getStudioTokenFromLocalStorage).toHaveBeenCalledWith(mockStorage, projectId)
expect(authState).toMatchObject({type: AuthStateType.LOGGED_IN, token: studioToken})
expect(options.authMethod).toBe('localstorage')
Expand All @@ -277,7 +277,7 @@ describe('authStore', () => {
})

// Initial state might be logged out before the async check completes
const {authState: initialAuthState} = authStore.getInitialState(instance)
const {authState: initialAuthState} = authStore.getInitialState(instance, null)
expect(initialAuthState.type).toBe(AuthStateType.LOGGED_OUT) // Or potentially logging in depending on other factors
expect(getStudioTokenFromLocalStorage).toHaveBeenCalledWith(mockStorage, projectId)
expect(checkForCookieAuth).toHaveBeenCalledWith(projectId, expect.any(Function))
Expand All @@ -297,7 +297,7 @@ describe('authStore', () => {
dataset: 'd',
})

const {authState, options} = authStore.getInitialState(instance)
const {authState, options} = authStore.getInitialState(instance, null)
expect(getStudioTokenFromLocalStorage).not.toHaveBeenCalled()
expect(checkForCookieAuth).not.toHaveBeenCalled()
expect(getTokenFromStorage).toHaveBeenCalled()
Expand All @@ -315,7 +315,7 @@ describe('authStore', () => {
vi.mocked(getAuthCode).mockReturnValue(null)
vi.mocked(getTokenFromLocation).mockReturnValue('hash-token')

const {authState} = authStore.getInitialState(instance)
const {authState} = authStore.getInitialState(instance, null)
expect(authState).toMatchObject({
type: AuthStateType.LOGGING_IN,
isExchangingToken: false,
Expand Down
32 changes: 16 additions & 16 deletions packages/core/src/auth/refreshStampedToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_IN,
token: 'sk-initial-token-st123',
Expand All @@ -131,7 +131,7 @@ describe('refreshStampedToken', () => {
initialState.dashboardContext = {mode: 'test'}
const state = createStoreState(initialState)

const subscription = refreshStampedToken({state, instance})
const subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription)

await vi.advanceTimersToNextTimerAsync()
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_IN,
token: 'sk-initial-token-st123',
Expand All @@ -177,7 +177,7 @@ describe('refreshStampedToken', () => {
initialState.dashboardContext = {mode: 'test'}
const state = createStoreState(initialState)

const subscription = refreshStampedToken({state, instance})
const subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription)

await vi.advanceTimersToNextTimerAsync()
Expand All @@ -202,7 +202,7 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_IN,
token: 'sk-initial-token-st123',
Expand All @@ -213,7 +213,7 @@ describe('refreshStampedToken', () => {
let subscription: Subscription | undefined
// We expect this NOT to throw, but accept we can't easily test the lock call or outcome
expect(() => {
subscription = refreshStampedToken({state, instance})
subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription!)
}).not.toThrow()

Expand Down Expand Up @@ -253,15 +253,15 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_IN,
token: 'sk-initial-token-st123',
currentUser: null,
}
const state = createStoreState(initialState)

const subscription = refreshStampedToken({state, instance})
const subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription)

// DO NOT advance timers or yield here - focus on immediate observable logic
Expand Down Expand Up @@ -303,15 +303,15 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_IN,
token: 'sk-initial-token-st123',
currentUser: null,
}
const state = createStoreState(initialState)

const subscription = refreshStampedToken({state, instance})
const subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription)

// Advance timers to allow the async `performRefresh` to execute
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_IN,
token: 'sk-initial-token-st123',
Expand All @@ -358,7 +358,7 @@ describe('refreshStampedToken', () => {
initialState.dashboardContext = {mode: 'test'}
const state = createStoreState(initialState)

const subscription = refreshStampedToken({state, instance})
const subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription)

await vi.advanceTimersToNextTimerAsync()
Expand All @@ -378,14 +378,14 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_OUT,
isDestroyingSession: false,
} as AuthState
const state = createStoreState(initialState)

const subscription = refreshStampedToken({state, instance})
const subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription)

await vi.advanceTimersByTimeAsync(0)
Expand All @@ -404,15 +404,15 @@ describe('refreshStampedToken', () => {
dataset: 'd',
auth: {clientFactory: mockClientFactory, storageArea: mockStorage},
})
const initialState = authStore.getInitialState(instance)
const initialState = authStore.getInitialState(instance, null)
initialState.authState = {
type: AuthStateType.LOGGED_IN,
token: 'sk-nonstamped-token',
currentUser: null,
}
const state = createStoreState(initialState)

const subscription = refreshStampedToken({state, instance})
const subscription = refreshStampedToken({state, instance, key: null})
subscriptions.push(subscription)

await vi.advanceTimersByTimeAsync(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
const clientFactory = vi.fn().mockReturnValue(mockClient)
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})

const state = createStoreState(authStore.getInitialState(instance))
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
const state = createStoreState(authStore.getInitialState(instance, null))
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})

expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})

Expand Down Expand Up @@ -52,8 +52,8 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
const clientFactory = vi.fn().mockReturnValue(mockClient)
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})

const state = createStoreState(authStore.getInitialState(instance))
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
const state = createStoreState(authStore.getInitialState(instance, null))
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})

expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})

Expand Down Expand Up @@ -88,8 +88,8 @@ describe('subscribeToStateAndFetchCurrentUser', () => {
const clientFactory = vi.fn().mockReturnValue(mockClient)
const instance = createSanityInstance({projectId: 'p', dataset: 'd', auth: {clientFactory}})

const state = createStoreState(authStore.getInitialState(instance))
const subscription = subscribeToStateAndFetchCurrentUser({state, instance})
const state = createStoreState(authStore.getInitialState(instance, null))
const subscription = subscribeToStateAndFetchCurrentUser({state, instance, key: null})

expect(state.get()).toMatchObject({authState: {type: AuthStateType.LOGGED_OUT}})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ describe('subscribeToStorageEventsAndSetToken', () => {
})

it('sets the state to logged in when a matching storage event returns a token', () => {
const state = createStoreState(authStore.getInitialState(instance))
const state = createStoreState(authStore.getInitialState(instance, null))
const {storageKey} = state.get().options
const subscription = subscribeToStorageEventsAndSetToken({state, instance})
const subscription = subscribeToStorageEventsAndSetToken({state, instance, key: null})

expect(state.get()).toMatchObject({
authState: {type: AuthStateType.LOGGED_OUT, isDestroyingSession: false},
Expand All @@ -54,10 +54,10 @@ describe('subscribeToStorageEventsAndSetToken', () => {

it('sets the state to logged in when a matching storage event returns null', () => {
vi.mocked(getTokenFromStorage).mockReturnValue('existing-token')
const state = createStoreState(authStore.getInitialState(instance))
const state = createStoreState(authStore.getInitialState(instance, null))
const {storageKey} = state.get().options

const subscription = subscribeToStorageEventsAndSetToken({state, instance})
const subscription = subscribeToStorageEventsAndSetToken({state, instance, key: null})

expect(state.get()).toMatchObject({
authState: {type: AuthStateType.LOGGED_IN, token: 'existing-token', currentUser: null},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('destroyController', () => {
})

// Execute action
destroyController({state, instance})
destroyController({state, instance, key: null})

// Verify controller was destroyed and state was cleared
expect(mockController.destroy).toHaveBeenCalled()
Expand All @@ -49,7 +49,7 @@ describe('destroyController', () => {

it('should do nothing if no controller exists', () => {
// State already has null controller, so just execute action
expect(() => destroyController({state, instance})).not.toThrow()
expect(() => destroyController({state, instance, key: null})).not.toThrow()

// State should remain unchanged
expect(state.get().controller).toBeNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('getOrCreateChannel', () => {
it('should create a new channel using the controller', () => {
const createChannelSpy = vi.spyOn(mockController, 'createChannel')

const channel = getOrCreateChannel({state, instance}, channelConfig)
const channel = getOrCreateChannel({state, instance, key: null}, channelConfig)

expect(createChannelSpy).toHaveBeenCalledWith(channelConfig)
expect(channel.on).toBeDefined()
Expand All @@ -70,17 +70,17 @@ describe('getOrCreateChannel', () => {
channels: new Map(),
})

expect(() => getOrCreateChannel({state, instance}, channelConfig)).toThrow(
expect(() => getOrCreateChannel({state, instance, key: null}, channelConfig)).toThrow(
'Controller must be initialized before using or creating channels',
)
})

it('should retrieve channel directly from store once created', () => {
const createdChannel = getOrCreateChannel({state, instance}, channelConfig)
const createdChannel = getOrCreateChannel({state, instance, key: null}, channelConfig)
vi.clearAllMocks() // Clear call counts

// Retrieve channel again
const retrievedChannel = getOrCreateChannel({state, instance}, channelConfig)
const retrievedChannel = getOrCreateChannel({state, instance, key: null}, channelConfig)
expect(retrievedChannel).toBeDefined()
expect(retrievedChannel).toBe(createdChannel)

Expand All @@ -95,10 +95,10 @@ describe('getOrCreateChannel', () => {
})

it('should throw error when trying to create channel with different options', () => {
getOrCreateChannel({state, instance}, channelConfig)
getOrCreateChannel({state, instance, key: null}, channelConfig)

expect(() =>
getOrCreateChannel({state, instance}, {...channelConfig, connectTo: 'window'}),
getOrCreateChannel({state, instance, key: null}, {...channelConfig, connectTo: 'window'}),
).toThrow('Channel "test" already exists with different options')
})
})
Loading
Loading