1010import { mockCoreContext } from '@kbn/core-base-server-mocks' ;
1111import { httpServerMock } from '@kbn/core-http-server-mocks' ;
1212import { userProfileServiceMock } from '@kbn/core-user-profile-server-mocks' ;
13- import type { UserProfileWithSecurity } from '@kbn/core-user-profile-common' ;
13+ import type { UserProfileWithSecurity , UserProfileData } from '@kbn/core-user-profile-common' ;
1414import { UserSettingsService } from './user_settings_service' ;
1515
1616describe ( '#setup' , ( ) => {
@@ -26,18 +26,18 @@ describe('#setup', () => {
2626 } ;
2727 } ) ;
2828
29- const createUserProfile = ( darkMode : string | undefined ) : UserProfileWithSecurity => {
29+ const createUserProfile = (
30+ userSettings : Partial < NonNullable < UserProfileData [ 'userSettings' ] > >
31+ ) : UserProfileWithSecurity => {
3032 return {
3133 data : {
32- userSettings : {
33- darkMode,
34- } ,
34+ userSettings,
3535 } ,
3636 } as unknown as UserProfileWithSecurity ;
3737 } ;
3838
3939 it ( 'fetches userSettings when client is set and returns `true` when `darkMode` is set to `dark`' , async ( ) => {
40- startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( 'dark' ) ) ;
40+ startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( { darkMode : 'dark' } ) ) ;
4141
4242 const { getUserSettingDarkMode } = service . setup ( ) ;
4343 service . start ( startDeps ) ;
@@ -54,7 +54,7 @@ describe('#setup', () => {
5454 } ) ;
5555
5656 it ( 'fetches userSettings when client is set and returns `false` when `darkMode` is set to `light`' , async ( ) => {
57- startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( 'light' ) ) ;
57+ startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( { darkMode : 'light' } ) ) ;
5858
5959 const { getUserSettingDarkMode } = service . setup ( ) ;
6060 service . start ( startDeps ) ;
@@ -71,7 +71,7 @@ describe('#setup', () => {
7171 } ) ;
7272
7373 it ( 'fetches userSettings when client is set and returns `system` when `darkMode` is set to `system`' , async ( ) => {
74- startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( 'system' ) ) ;
74+ startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( { darkMode : 'system' } ) ) ;
7575
7676 const { getUserSettingDarkMode } = service . setup ( ) ;
7777 service . start ( startDeps ) ;
@@ -88,7 +88,7 @@ describe('#setup', () => {
8888 } ) ;
8989
9090 it ( 'fetches userSettings when client is set and returns `undefined` when `darkMode` is set to `` (the default value)' , async ( ) => {
91- startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( '' ) ) ;
91+ startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( { darkMode : undefined } ) ) ;
9292
9393 const { getUserSettingDarkMode } = service . setup ( ) ;
9494 service . start ( startDeps ) ;
@@ -105,7 +105,9 @@ describe('#setup', () => {
105105 } ) ;
106106
107107 it ( 'fetches userSettings when client is set and returns `undefined` when `darkMode` is set to `space_default`' , async ( ) => {
108- startDeps . userProfile . getCurrent . mockResolvedValue ( createUserProfile ( 'space_default' ) ) ;
108+ startDeps . userProfile . getCurrent . mockResolvedValue (
109+ createUserProfile ( { darkMode : 'space_default' } )
110+ ) ;
109111
110112 const { getUserSettingDarkMode } = service . setup ( ) ;
111113 service . start ( startDeps ) ;
@@ -121,6 +123,63 @@ describe('#setup', () => {
121123 } ) ;
122124 } ) ;
123125
126+ it ( 'fetches userSettings when client is set and returns `true` when `rememberSelectedSpace` is set to `true`' , async ( ) => {
127+ startDeps . userProfile . getCurrent . mockResolvedValue (
128+ createUserProfile ( { rememberSelectedSpace : true } )
129+ ) ;
130+
131+ const { getUserSettingRememberSelectedSpace } = service . setup ( ) ;
132+ service . start ( startDeps ) ;
133+
134+ const kibanaRequest = httpServerMock . createKibanaRequest ( ) ;
135+ const rememberSelectedSpace = await getUserSettingRememberSelectedSpace ( kibanaRequest ) ;
136+
137+ expect ( rememberSelectedSpace ) . toEqual ( true ) ;
138+ expect ( startDeps . userProfile . getCurrent ) . toHaveBeenCalledTimes ( 1 ) ;
139+ expect ( startDeps . userProfile . getCurrent ) . toHaveBeenCalledWith ( {
140+ request : kibanaRequest ,
141+ dataPath : 'userSettings' ,
142+ } ) ;
143+ } ) ;
144+
145+ it ( 'fetches userSettings when client is set and returns `false` when `rememberSelectedSpace` is set to `false`' , async ( ) => {
146+ startDeps . userProfile . getCurrent . mockResolvedValue (
147+ createUserProfile ( { rememberSelectedSpace : false } )
148+ ) ;
149+
150+ const { getUserSettingRememberSelectedSpace } = service . setup ( ) ;
151+ service . start ( startDeps ) ;
152+
153+ const kibanaRequest = httpServerMock . createKibanaRequest ( ) ;
154+ const rememberSelectedSpace = await getUserSettingRememberSelectedSpace ( kibanaRequest ) ;
155+
156+ expect ( rememberSelectedSpace ) . toEqual ( false ) ;
157+ expect ( startDeps . userProfile . getCurrent ) . toHaveBeenCalledTimes ( 1 ) ;
158+ expect ( startDeps . userProfile . getCurrent ) . toHaveBeenCalledWith ( {
159+ request : kibanaRequest ,
160+ dataPath : 'userSettings' ,
161+ } ) ;
162+ } ) ;
163+
164+ it ( 'fetches userSettings when client is set and returns `true` when `rememberSelectedSpace` is not set' , async ( ) => {
165+ startDeps . userProfile . getCurrent . mockResolvedValue (
166+ createUserProfile ( { rememberSelectedSpace : undefined } )
167+ ) ;
168+
169+ const { getUserSettingRememberSelectedSpace } = service . setup ( ) ;
170+ service . start ( startDeps ) ;
171+
172+ const kibanaRequest = httpServerMock . createKibanaRequest ( ) ;
173+ const rememberSelectedSpace = await getUserSettingRememberSelectedSpace ( kibanaRequest ) ;
174+
175+ expect ( rememberSelectedSpace ) . toEqual ( true ) ;
176+ expect ( startDeps . userProfile . getCurrent ) . toHaveBeenCalledTimes ( 1 ) ;
177+ expect ( startDeps . userProfile . getCurrent ) . toHaveBeenCalledWith ( {
178+ request : kibanaRequest ,
179+ dataPath : 'userSettings' ,
180+ } ) ;
181+ } ) ;
182+
124183 it ( 'does not fetch userSettings when client is not set, returns `undefined`, and logs a debug statement' , async ( ) => {
125184 const { getUserSettingDarkMode } = service . setup ( ) ;
126185
0 commit comments