Skip to content
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

Merged
merged 6 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
17 changes: 17 additions & 0 deletions packages/embed-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,25 @@
},
"homepage": "https://github.com/looker-open-source/sdk-codegen/tree/master/packages/embed-components",
"devDependencies": {
"redux-saga-tester": "^1.0.874",
"@looker/sdk-node": "^23.4.0",
"@testing-library/react": "^11.2.7",
"@looker/components-test-utils": "^1.5.27",
"react-redux": "^7.2.9",
Copy link
Contributor Author

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

"@types/react-redux": "^7.1.25",
"@testing-library/user-event": "^14.4.3"
},
"dependencies": {
"@looker/components": "^4.1.3",
"@looker/embed-services": "0.0.1-alpha",
"@looker/redux": "0.0.0",
"@looker/sdk": "^23.2.0",
"@reduxjs/toolkit": "^1.9.3",
"@styled-icons/material-outlined": "^10.47.0",
"react": "16.14.0",
"react-dom": "16.14.0",
"styled-components": "^5.3.1",
"typed-redux-saga": "^1.5.0"
},
"keywords": [
"Looker",
Expand Down
27 changes: 27 additions & 0 deletions packages/embed-components/src/GlobalStore/index.ts
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'
92 changes: 92 additions & 0 deletions packages/embed-components/src/GlobalStore/sagas.spec.ts
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 })
)
})
})
})
46 changes: 46 additions & 0 deletions packages/embed-components/src/GlobalStore/sagas.ts
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this once outside of the saga.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 factoryActions got destructured before the slice was built. I'll try again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I get this:

    TypeError: Cannot destructure property 'initAction' of '_slice.themeActions' as it is undefined.

      36 |
      37 | const {
    > 38 |   initAction,
         |   ^
      39 |   loadThemeDataAction,
      40 |   getThemesAction,
      41 |   getDefaultThemeAction

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing this when I try it in GlobalStore

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing your issue

yield* takeEvery(initFactoryAction, initSaga)
}
72 changes: 72 additions & 0 deletions packages/embed-components/src/GlobalStore/slice.ts
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)
46 changes: 46 additions & 0 deletions packages/embed-components/src/GlobalStore/store.ts
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
}
Loading