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

refactor: Use useSettings hook in DefaultRedirectionSnackbar #2117

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"homepage": "https://github.com/cozy/cozy-home#readme",
"dependencies": {
"@cozy/minilog": "1.0.0",
"cozy-client": "^45.14.1",
"cozy-client": "^46.9.0",
"cozy-device-helper": "2.7.0",
"cozy-doctypes": "1.83.8",
"cozy-flags": "3.2.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react'

import { useClient, useQuery } from 'cozy-client'
import { useSettings } from 'cozy-client'
import { useWebviewIntent } from 'cozy-intent'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'
import { makeStyles } from 'cozy-ui/transpiled/react/styles'
Expand All @@ -10,13 +10,12 @@ import Button from 'cozy-ui/transpiled/react/Buttons'
import Icon from 'cozy-ui/transpiled/react/Icon'
import LightbulbIcon from 'cozy-ui/transpiled/react/Icons/Lightbulb'

import { instanceSettingsConn, homeSettingsConn } from 'queries'
import {
shouldShowDefaultRedirectionSnackbar,
disableDefaultRedirectionSnackbar,
setDefaultRedirectionToHome
} from './helpers'
HOME_DEFAULT_REDIRECTION,
useShouldShowDefaultRedirectionSnackbar
} from './useShouldShowDefaultRedirectionSnackbar'
import useIncrementDefaultRedirectionViewCount from './useIncrementDefaultRedirectionViewCount'
import { isFlagshipApp } from 'cozy-device-helper'

const useStyles = makeStyles(theme => ({
snackbar: {
Expand All @@ -29,39 +28,39 @@ const useStyles = makeStyles(theme => ({

const DefaultAppSnackbar = () => {
const { t } = useI18n()
const client = useClient()
const classes = useStyles()
const [isOpen, setIsOpen] = useState(true)

const webviewIntent = useWebviewIntent()

const instanceSettingsResult = useQuery(
instanceSettingsConn.query,
instanceSettingsConn
)
const { save: saveHome } = useSettings('home', [
'default_redirection_snackbar_disabled'
])

const homeSettingsResult = useQuery(homeSettingsConn.query, homeSettingsConn)
const { save: saveGlobal } = useSettings('instance', ['default_redirection'])

useIncrementDefaultRedirectionViewCount(
instanceSettingsResult,
homeSettingsResult
)
useIncrementDefaultRedirectionViewCount()

const showDefaultAppSnackbar = shouldShowDefaultRedirectionSnackbar(
instanceSettingsResult,
homeSettingsResult,
isOpen
)
const showDefaultAppSnackbar = useShouldShowDefaultRedirectionSnackbar(isOpen)

const onRefuse = () => {
setIsOpen(false)
disableDefaultRedirectionSnackbar(client, homeSettingsResult)
saveHome({
default_redirection_snackbar_disabled: true
})
}

const onAccept = () => {
setIsOpen(false)
disableDefaultRedirectionSnackbar(client, homeSettingsResult)
setDefaultRedirectionToHome(client, instanceSettingsResult, webviewIntent)
saveHome({
default_redirection_snackbar_disabled: true
})
saveGlobal({
default_redirection: HOME_DEFAULT_REDIRECTION
})
if (isFlagshipApp()) {
webviewIntent.call('setDefaultRedirection', HOME_DEFAULT_REDIRECTION)
}
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import React from 'react'
import { render, fireEvent } from '@testing-library/react'

import { useSettings } from 'cozy-client'

import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme'

import AppLike from 'test/AppLike'
import DefaultRedirectionSnackbar from './DefaultRedirectionSnackbar'
import {
shouldShowDefaultRedirectionSnackbar,
disableDefaultRedirectionSnackbar,
setDefaultRedirectionToHome
} from './helpers'
import { useShouldShowDefaultRedirectionSnackbar } from './useShouldShowDefaultRedirectionSnackbar'

jest.mock('cozy-client', () => ({
...jest.requireActual('cozy-client'),
useQuery: jest.fn(),
useClient: jest.fn()
useClient: jest.fn(),
useSettings: jest.fn()
}))
jest.mock('./helpers')
jest.mock('./useShouldShowDefaultRedirectionSnackbar')
jest.mock('./useIncrementDefaultRedirectionViewCount')

const setup = () => {
Expand All @@ -36,7 +35,26 @@ describe('DefaultRedirectionSnackbar', () => {
})

it('should display default redirection snackbar', () => {
shouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
useShouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
const mockSaveInstance = jest.fn()
const mockSaveHome = jest.fn()
useSettings.mockImplementation((...args) => {
if (args[0] === 'instance') {
return {
values: {
default_redirection: 'drive/#/folder'
},
save: mockSaveInstance
}
} else if (args[0] === 'home') {
return {
values: {
default_redirection_view_count: 4
},
save: mockSaveHome
}
}
})

const { root } = setup()

Expand All @@ -45,24 +63,70 @@ describe('DefaultRedirectionSnackbar', () => {
})

it('should disable default redirection snackbar and set default redirection to home when accepting', () => {
shouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
useShouldShowDefaultRedirectionSnackbar.mockReturnValue(true)

const mockSaveInstance = jest.fn()
const mockSaveHome = jest.fn()
useSettings.mockImplementation((...args) => {
if (args[0] === 'instance') {
return {
values: {
default_redirection: 'drive/#/folder'
},
save: mockSaveInstance
}
} else if (args[0] === 'home') {
return {
values: {
default_redirection_view_count: 4
},
save: mockSaveHome
}
}
})

const { root } = setup()

fireEvent.click(root.queryByText('OK'))

expect(disableDefaultRedirectionSnackbar).toHaveBeenCalled()
expect(setDefaultRedirectionToHome).toHaveBeenCalled()
expect(mockSaveInstance).toHaveBeenCalledWith({
default_redirection: 'home/'
})
expect(mockSaveHome).toHaveBeenCalledWith({
default_redirection_snackbar_disabled: true
})
})

it('should disable default redirection snackbar when refusing', () => {
shouldShowDefaultRedirectionSnackbar.mockReturnValue(true)
useShouldShowDefaultRedirectionSnackbar.mockReturnValue(true)

const mockSaveInstance = jest.fn()
const mockSaveHome = jest.fn()
useSettings.mockImplementation((...args) => {
if (args[0] === 'instance') {
return {
values: {
default_redirection: 'drive/#/folder'
},
save: mockSaveInstance
}
} else if (args[0] === 'home') {
return {
values: {
default_redirection_view_count: 4
},
save: mockSaveHome
}
}
})

const { root } = setup()

fireEvent.click(root.queryByText('No, thank you'))

expect(disableDefaultRedirectionSnackbar).toHaveBeenCalled()
expect(setDefaultRedirectionToHome).not.toHaveBeenCalled()
expect(mockSaveHome).toHaveBeenCalledWith({
default_redirection_snackbar_disabled: true
})
expect(mockSaveInstance).not.toHaveBeenCalled()
})
})
107 changes: 0 additions & 107 deletions src/components/DefaultRedirectionSnackbar/helpers.js

This file was deleted.

Loading
Loading