-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: SelectTheme component #1300
Changes from 4 commits
5eeaaae
c22733a
702b4e2
8bdd542
ac252f3
e59784d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2023 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
export * from './slice' | ||
export * from './store' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2023 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import ReduxSagaTester from 'redux-saga-tester' | ||
import { getFactory, createFactory } from '@looker/embed-services' | ||
import type { IAPIMethods } from '@looker/sdk-rtl' | ||
import { | ||
factoryActions, | ||
FACTORY_SLICE_NAME, | ||
factorySlice, | ||
defaultFactoryState, | ||
} from './slice' | ||
import * as sagas from './sagas' | ||
|
||
jest.mock('@looker/embed-services', () => ({ | ||
...jest.requireActual('@looker/embed-services'), | ||
createFactory: jest.fn(), | ||
})) | ||
|
||
describe('Factory sagas', () => { | ||
let sagaTester: ReduxSagaTester<any> | ||
const mockSdk = {} as IAPIMethods | ||
const { initFactoryAction, initFactorySuccessAction, setFailureAction } = | ||
factoryActions | ||
|
||
beforeEach(() => { | ||
sagaTester = new ReduxSagaTester({ | ||
initialState: { [FACTORY_SLICE_NAME]: defaultFactoryState }, | ||
reducers: { | ||
[FACTORY_SLICE_NAME]: factorySlice.reducer, | ||
}, | ||
}) | ||
sagaTester.start(sagas.saga) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('initSaga', () => { | ||
it('sends initFactorySuccessAction on success', async () => { | ||
expect(getFactory).toThrow('Factory must be created with an SDK.') | ||
|
||
sagaTester.dispatch(initFactoryAction({ sdk: mockSdk })) | ||
|
||
await sagaTester.waitFor('factory/initFactorySuccessAction') | ||
|
||
const calledActions = sagaTester.getCalledActions() | ||
expect(calledActions).toHaveLength(2) | ||
expect(calledActions[0]).toEqual(initFactoryAction({ sdk: mockSdk })) | ||
expect(calledActions[1]).toEqual(initFactorySuccessAction()) | ||
}) | ||
|
||
it('sends setFailureAction on error', async () => { | ||
const expectedError = 'Failed to create factory' | ||
;(createFactory as jest.Mock).mockImplementationOnce(() => { | ||
throw new Error(expectedError) | ||
}) | ||
sagaTester.dispatch(initFactoryAction({ sdk: mockSdk })) | ||
|
||
await sagaTester.waitFor('factory/setFailureAction') | ||
const calledActions = sagaTester.getCalledActions() | ||
expect(calledActions).toHaveLength(2) | ||
expect(calledActions[0]).toEqual(initFactoryAction({ sdk: mockSdk })) | ||
expect(calledActions[1]).toEqual( | ||
setFailureAction({ error: expectedError }) | ||
) | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2023 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import { takeEvery, put } from 'typed-redux-saga' | ||
import { createFactory } from '@looker/embed-services' | ||
import type { PayloadAction } from '@reduxjs/toolkit' | ||
|
||
import { factoryActions } from './slice' | ||
import type { InitFactoryAction } from './slice' | ||
|
||
function* initSaga(action: PayloadAction<InitFactoryAction>) { | ||
const { initFactorySuccessAction, setFailureAction } = factoryActions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do this once outside of the saga. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, if I remember correctly I tried this when writing this and it failed because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I get this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing this when I try it in GlobalStore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not seeing it in theme sagas either |
||
try { | ||
createFactory(action.payload.sdk) | ||
yield* put(initFactorySuccessAction()) | ||
} catch (error: any) { | ||
yield* put(setFailureAction({ error: error.message })) | ||
} | ||
} | ||
|
||
export function* saga() { | ||
const { initFactoryAction } = factoryActions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can extract the actions outside of the saga and just do it once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing your issue |
||
yield* takeEvery(initFactoryAction, initSaga) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2023 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import { createSlice } from '@reduxjs/toolkit' | ||
import { createSliceHooks } from '@looker/redux' | ||
import type { IAPIMethods } from '@looker/sdk-rtl' | ||
import type { PayloadAction } from '@reduxjs/toolkit' | ||
import { saga } from './sagas' | ||
|
||
export interface FactoryState { | ||
initialized: boolean | ||
error?: string | ||
} | ||
|
||
export const defaultFactoryState: FactoryState = { | ||
initialized: false, | ||
} | ||
|
||
export interface InitFactoryAction { | ||
sdk: IAPIMethods | ||
} | ||
|
||
type SetFailureAction = Record<'error', string> | ||
|
||
export const FACTORY_SLICE_NAME = 'factory' | ||
|
||
export const factorySlice = createSlice({ | ||
name: FACTORY_SLICE_NAME, | ||
initialState: defaultFactoryState, | ||
reducers: { | ||
initFactoryAction(_state, _action: PayloadAction<InitFactoryAction>) { | ||
// noop | ||
}, | ||
initFactorySuccessAction(state) { | ||
state.initialized = true | ||
}, | ||
destroyFactoryAction() { | ||
// noop | ||
}, | ||
setFailureAction(state, action: PayloadAction<SetFailureAction>) { | ||
state.error = action.payload.error | ||
}, | ||
}, | ||
}) | ||
|
||
export const factoryActions = factorySlice.actions | ||
export const { | ||
useActions: useFactoryActions, | ||
useStoreState: useFactoryStoreState, | ||
} = createSliceHooks(factorySlice, saga) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2023 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import { createStore } from '@looker/redux' | ||
import { defaultThemesState, themesSlice } from '../Theme' | ||
import type { ThemesState } from '../Theme' | ||
import { defaultFactoryState, factorySlice } from './slice' | ||
import type { FactoryState } from './slice' | ||
|
||
export const store = createStore({ | ||
preloadedState: { | ||
factory: defaultFactoryState, | ||
themes: defaultThemesState, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to see an experiment were the store is created empty and the slices are added dynamically. I suspect it wont work because of the deep combine issue causing a re-render but I'd like to see what happens. If this works, this would allow the developer to only load the slices they need |
||
}, | ||
reducer: { | ||
factory: factorySlice.reducer, | ||
themes: themesSlice.reducer, | ||
}, | ||
}) | ||
|
||
export interface RootState { | ||
factory: FactoryState | ||
themes?: ThemesState | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed as a dev dependency because
Provider
is used in tests