Skip to content

Commit

Permalink
fix(core): disable scheduledPublishing and tasks plugins if features …
Browse files Browse the repository at this point in the history
…api returns error
  • Loading branch information
pedrobonamin committed Sep 17, 2024
1 parent daf4ffa commit 2455028
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ describe('ScheduledPublishingEnabledProvider', () => {
expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should not show the plugin if useFeatureEnabled has an error', () => {
require('../../../hooks').useFeatureEnabled.mockReturnValue({
enabled: false,
isLoading: true,
error: new Error('Something went wrong'),
})
require('../../../studio').useWorkspace.mockReturnValue({scheduledPublishing: {enabled: true}})

const value = renderHook(useScheduledPublishingEnabled, {
wrapper: ScheduledPublishingEnabledProvider,
})

expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should call "useFeatureEnabled" with "scheduledPublishing"', () => {
require('../../../studio').useWorkspace.mockReturnValue({scheduledPublishing: {enabled: false}})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ interface TaksEnabledProviderProps {
*/

export function ScheduledPublishingEnabledProvider({children}: TaksEnabledProviderProps) {
const {enabled, isLoading} = useFeatureEnabled('scheduledPublishing')
const {enabled, isLoading, error} = useFeatureEnabled('scheduledPublishing')
const {scheduledPublishing} = useWorkspace()

const isWorkspaceEnabled = scheduledPublishing.enabled

const value: ScheduledPublishingEnabledContextValue = useMemo(() => {
if (!isWorkspaceEnabled || isLoading) {
if (!isWorkspaceEnabled || isLoading || error) {
return {
enabled: false,
mode: null,
Expand All @@ -42,7 +42,7 @@ export function ScheduledPublishingEnabledProvider({children}: TaksEnabledProvid
enabled: true,
mode: enabled ? 'default' : 'upsell',
}
}, [enabled, isLoading, isWorkspaceEnabled])
}, [enabled, isLoading, isWorkspaceEnabled, error])

return (
<ScheduledPublishingEnabledContext.Provider value={value}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ describe('TasksEnabledProvider', () => {
expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should not show the plugin if useFeatureEnabled has an error', () => {
require('../../../hooks').useFeatureEnabled.mockReturnValue({
enabled: false,
isLoading: true,
error: new Error('Something went wrong'),
})
require('../../../studio').useWorkspace.mockReturnValue({tasks: {enabled: true}})

const value = renderHook(useTasksEnabled, {wrapper: TasksEnabledProvider})

expect(value.result.current).toEqual({enabled: false, mode: null})
})

it('should call "useFeatureEnabled" with "sanityTasks"', () => {
require('../../../studio').useWorkspace.mockReturnValue({tasks: {enabled: false}})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ interface TaksEnabledProviderProps {
* @internal
*/
export function TasksEnabledProvider({children}: TaksEnabledProviderProps) {
const {enabled, isLoading} = useFeatureEnabled('sanityTasks')
const {enabled, isLoading, error} = useFeatureEnabled('sanityTasks')

const isWorkspaceEnabled = useWorkspace().tasks?.enabled

const value: TasksEnabledContextValue = useMemo(() => {
if (!isWorkspaceEnabled || isLoading) {
if (!isWorkspaceEnabled || isLoading || error) {
return {
enabled: false,
mode: null,
Expand All @@ -28,7 +28,7 @@ export function TasksEnabledProvider({children}: TaksEnabledProviderProps) {
enabled: true,
mode: enabled ? 'default' : 'upsell',
}
}, [enabled, isLoading, isWorkspaceEnabled])
}, [enabled, isLoading, isWorkspaceEnabled, error])

return <TasksEnabledContext.Provider value={value}>{children}</TasksEnabledContext.Provider>
}

0 comments on commit 2455028

Please sign in to comment.