|
| 1 | +import { createSlice, PayloadAction } from '@reduxjs/toolkit'; |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | +import type { DaVinciConfig } from './config.types.js'; |
| 4 | + |
| 5 | +// Assuming the initialState is as follows: |
| 6 | +const initialState = { |
| 7 | + clientId: '', |
| 8 | + redirectUri: '', |
| 9 | + responseType: '', |
| 10 | + scope: '', |
| 11 | + serverConfig: { |
| 12 | + baseUrl: '', |
| 13 | + }, |
| 14 | +}; |
| 15 | + |
| 16 | +export const configSlice = createSlice({ |
| 17 | + name: 'config', |
| 18 | + initialState, |
| 19 | + reducers: { |
| 20 | + set(state, action: PayloadAction<DaVinciConfig>) { |
| 21 | + state.clientId = action.payload.clientId || ''; |
| 22 | + state.redirectUri = action.payload.redirectUri || `${location.origin}/handle-redirect`; |
| 23 | + state.responseType = action.payload.responseType || 'code'; |
| 24 | + state.scope = action.payload.scope || 'openid'; |
| 25 | + |
| 26 | + if (!action.payload.serverConfig?.baseUrl) { |
| 27 | + state.serverConfig = { |
| 28 | + baseUrl: '', |
| 29 | + }; |
| 30 | + } else if (action.payload.serverConfig?.baseUrl.endsWith('/')) { |
| 31 | + state.serverConfig = { |
| 32 | + baseUrl: action.payload.serverConfig?.baseUrl, |
| 33 | + }; |
| 34 | + } else { |
| 35 | + state.serverConfig = { |
| 36 | + baseUrl: action.payload.serverConfig?.baseUrl.concat('/'), |
| 37 | + }; |
| 38 | + } |
| 39 | + }, |
| 40 | + }, |
| 41 | +}); |
| 42 | + |
| 43 | +describe('configSlice', () => { |
| 44 | + const { set } = configSlice.actions; |
| 45 | + |
| 46 | + it('should set the configuration correctly with a complete action', () => { |
| 47 | + const action = set({ |
| 48 | + clientId: 'test-client-id', |
| 49 | + redirectUri: 'http://example.com/redirect', |
| 50 | + responseType: 'token', |
| 51 | + scope: 'profile', |
| 52 | + serverConfig: { |
| 53 | + baseUrl: 'http://server.com/api', |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + const expectedState = { |
| 58 | + clientId: 'test-client-id', |
| 59 | + redirectUri: 'http://example.com/redirect', |
| 60 | + responseType: 'token', |
| 61 | + scope: 'profile', |
| 62 | + serverConfig: { |
| 63 | + baseUrl: 'http://server.com/api/', |
| 64 | + }, |
| 65 | + }; |
| 66 | + |
| 67 | + expect(configSlice.reducer(initialState, action)).toEqual(expectedState); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should use default values when no values are provided', () => { |
| 71 | + const action = set({}); |
| 72 | + const expectedState = { |
| 73 | + clientId: '', |
| 74 | + redirectUri: `${location.origin}/handle-redirect`, |
| 75 | + responseType: 'code', |
| 76 | + scope: 'openid', |
| 77 | + serverConfig: { |
| 78 | + baseUrl: '', |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + expect(configSlice.reducer(initialState, action)).toEqual(expectedState); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should set serverConfig baseUrl correctly when baseUrl does not end with a slash', () => { |
| 86 | + const action = set({ |
| 87 | + serverConfig: { |
| 88 | + baseUrl: 'http://server.com/am', |
| 89 | + }, |
| 90 | + }); |
| 91 | + |
| 92 | + const expectedState = { |
| 93 | + clientId: '', |
| 94 | + redirectUri: `${location.origin}/handle-redirect`, |
| 95 | + responseType: 'code', |
| 96 | + scope: 'openid', |
| 97 | + serverConfig: { |
| 98 | + baseUrl: 'http://server.com/am/', |
| 99 | + }, |
| 100 | + }; |
| 101 | + |
| 102 | + expect(configSlice.reducer(initialState, action)).toEqual(expectedState); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should set serverConfig baseUrl correctly when baseUrl ends with a slash', () => { |
| 106 | + const action = set({ |
| 107 | + serverConfig: { |
| 108 | + baseUrl: 'http://server.com/am/', |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + const expectedState = { |
| 113 | + clientId: '', |
| 114 | + redirectUri: `${location.origin}/handle-redirect`, |
| 115 | + responseType: 'code', |
| 116 | + scope: 'openid', |
| 117 | + serverConfig: { |
| 118 | + baseUrl: 'http://server.com/am/', |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + expect(configSlice.reducer(initialState, action)).toEqual(expectedState); |
| 123 | + }); |
| 124 | +}); |
0 commit comments