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

Add nohoist for cozy-ui #2605

Merged
merged 2 commits into from
Nov 7, 2024
Merged
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
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"./packages/commitlint-config-cozy"
]
},
"workspaces": [
"packages/*"
]
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": ["**/cozy-ui"]
}
}
73 changes: 73 additions & 0 deletions packages/cozy-viewer/src/ViewersByFile/ImageViewer.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { render, waitFor } from '@testing-library/react'
import React from 'react'

import { checkImageSource } from 'cozy-ui/transpiled/react/FileImageLoader/checkImageSource'
import { BreakpointsProvider } from 'cozy-ui/transpiled/react/providers/Breakpoints'
import EncryptedProvider from 'cozy-ui/transpiled/react/providers/Encrypted'

import ImageViewer from './ImageViewer'
import DemoProvider from '../providers/DemoProvider'

jest.mock('cozy-ui/transpiled/react/FileImageLoader/checkImageSource', () => ({
...jest.requireActual(
'cozy-ui/transpiled/react/FileImageLoader/checkImageSource'
),
checkImageSource: jest.fn()
}))

const file = {
_id: 'image',
class: 'image',
mime: 'image/jpg',
name: 'sample.jpg',
links: {
large: 'https://viewerdemo.cozycloud.cc/IMG_0062.PNG'
}
}

const gestures = {
on: jest.fn(),
off: jest.fn(),
get: jest.fn()
}

const setup = () => {
const root = render(
<DemoProvider>
<BreakpointsProvider>
<EncryptedProvider>
<ImageViewer
file={file}
gestures={gestures}
gesturesRef={{}}
onSwipe={jest.fn()}
/>
</EncryptedProvider>
</BreakpointsProvider>
</DemoProvider>
)

return { root }
}

describe('ImageViewer', () => {
afterEach(() => {
jest.restoreAllMocks
})

it('should render a spinner then the image viewer', async () => {
const { root } = setup()
const { container, queryByRole } = root

expect(queryByRole('progressbar')).toBeTruthy()

// simulate a successfull image loading
checkImageSource.mockResolvedValue('ok')

await waitFor(() => container.querySelector('img'))

const img = container.querySelector('img')
expect(img.getAttribute('alt')).toBe(file.name)
expect(img.getAttribute('src')).toBe(file.links.large)
})
})
76 changes: 76 additions & 0 deletions packages/cozy-viewer/src/ViewersByFile/PdfMobileViewer.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { render, waitFor } from '@testing-library/react'
import React from 'react'

import { CozyProvider, createMockClient } from 'cozy-client'
import logger from 'cozy-logger'
import EncryptedProvider from 'cozy-ui/transpiled/react/providers/Encrypted'
import { I18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { PdfMobileViewer } from './PdfMobileViewer'

logger.error = logger.warn = jest.fn()

const client = createMockClient({})
client.collection = jest.fn(() => ({
getDownloadLinkById: jest.fn()
}))
client.plugins.realtime = {
subscribe: jest.fn(),
unsubscribe: jest.fn(),
unsubscribeAll: jest.fn()
}
jest.mock('cozy-ui/transpiled/react/providers/Alert', () => ({
useAlert: jest.fn().mockReturnValue({ showAlert: jest.fn() })
}))

const file = {
_id: 'pdf',
class: 'pdf',
name: 'Demo.pdf',
mime: 'application/pdf',
links: {
medium: 'https://viewerdemo.cozycloud.cc/IMG_0062.PNG'
}
}

const setup = ({ file }) => {
const root = render(
<CozyProvider client={client}>
<I18n lang="en" dictRequire={() => ''}>
<EncryptedProvider>
<PdfMobileViewer file={file} t={x => x} />
</EncryptedProvider>
</I18n>
</CozyProvider>
)

return { root }
}

describe('PdfMobileViewer', () => {
it('should show a spinner if image is not loaded', () => {
const { root } = setup({ file })
const { getByRole } = root

expect(getByRole('progressbar'))
})

describe('errors if file as no medium or failed to download', () => {
let fileWithoutLinks = file

beforeAll(() => {
fileWithoutLinks.links = undefined
})

it('should show "download" button on browser', async () => {
const { root } = setup({ file: fileWithoutLinks })
const { getByText, queryByRole } = root

await waitFor(() => {
expect(queryByRole('progressbar')).toBeFalsy()
expect(getByText('Viewer.download'))
expect(getByText(file.name))
})
})
})
})
Loading