chore[GH-#851]: Add unit tests for Gallery (#1577)

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
Julien Fabre 2021-10-18 20:53:36 +02:00 committed by GitHub
parent 0488170beb
commit 85ffb63b91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 373 additions and 0 deletions

View file

@ -0,0 +1,247 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`src/components/gallery/Gallery return Gallery and click new 1`] = `
<div>
<div
class="Gallery"
>
<div
class="GalleryCard selected"
draggable="true"
style="opacity: 1;"
>
<div
aria-label="menuwrapper"
class="MenuWrapper optionsMenu"
role="button"
>
<div
class="Button IconButton"
role="button"
>
<i
class="CompassIcon icon-dots-horizontal OptionsIcon"
/>
</div>
</div>
<div
class="gallery-item"
/>
</div>
<div
class="GalleryCard"
draggable="true"
style="opacity: 1;"
>
<div
aria-label="menuwrapper"
class="MenuWrapper optionsMenu"
role="button"
>
<div
class="Button IconButton"
role="button"
>
<i
class="CompassIcon icon-dots-horizontal OptionsIcon"
/>
</div>
</div>
<div
class="gallery-item"
/>
</div>
<div
class="octo-gallery-new"
>
+ New
</div>
</div>
</div>
`;
exports[`src/components/gallery/Gallery return Gallery readonly 1`] = `
<div>
<div
class="Gallery"
>
<div
class="GalleryCard selected"
draggable="true"
style="opacity: 1;"
>
<div
class="gallery-item"
/>
</div>
<div
class="GalleryCard"
draggable="true"
style="opacity: 1;"
>
<div
class="gallery-item"
/>
</div>
</div>
</div>
`;
exports[`src/components/gallery/Gallery should match snapshot 1`] = `
<div>
<div
class="Gallery"
>
<div
class="GalleryCard selected"
draggable="true"
style="opacity: 1;"
>
<div
aria-label="menuwrapper"
class="MenuWrapper optionsMenu"
role="button"
>
<div
class="Button IconButton"
role="button"
>
<i
class="CompassIcon icon-dots-horizontal OptionsIcon"
/>
</div>
<div
class="Menu noselect left"
>
<div
class="menu-contents"
>
<div
class="menu-options"
>
<div
aria-label="Delete"
class="MenuOption TextOption menu-option"
role="button"
>
<svg
class="DeleteIcon Icon"
viewBox="0 0 448 512"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M268 416h24a12 12 0 0 0 12-12V188a12 12 0 0 0-12-12h-24a12 12 0 0 0-12 12v216a12 12 0 0 0 12 12zM432 80h-82.41l-34-56.7A48 48 0 0 0 274.41 0H173.59a48 48 0 0 0-41.16 23.3L98.41 80H16A16 16 0 0 0 0 96v16a16 16 0 0 0 16 16h16v336a48 48 0 0 0 48 48h288a48 48 0 0 0 48-48V128h16a16 16 0 0 0 16-16V96a16 16 0 0 0-16-16zM171.84 50.91A6 6 0 0 1 177 48h94a6 6 0 0 1 5.15 2.91L293.61 80H154.39zM368 464H80V128h288zm-212-48h24a12 12 0 0 0 12-12V188a12 12 0 0 0-12-12h-24a12 12 0 0 0-12 12v216a12 12 0 0 0 12 12z"
/>
</svg>
<div
class="menu-name"
>
Delete
</div>
<div
class="noicon"
/>
</div>
<div
aria-label="Duplicate"
class="MenuOption TextOption menu-option"
role="button"
>
<svg
class="DuplicateIcon Icon"
viewBox="0 0 512 512"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M464 0H144c-26.51 0-48 21.49-48 48v48H48c-26.51 0-48 21.49-48 48v320c0 26.51 21.49 48 48 48h320c26.51 0 48-21.49 48-48v-48h48c26.51 0 48-21.49 48-48V48c0-26.51-21.49-48-48-48zM362 464H54a6 6 0 0 1-6-6V150a6 6 0 0 1 6-6h42v224c0 26.51 21.49 48 48 48h224v42a6 6 0 0 1-6 6zm96-96H150a6 6 0 0 1-6-6V54a6 6 0 0 1 6-6h308a6 6 0 0 1 6 6v308a6 6 0 0 1-6 6z"
/>
</svg>
<div
class="menu-name"
>
Duplicate
</div>
<div
class="noicon"
/>
</div>
<div
aria-label="Copy link"
class="MenuOption TextOption menu-option"
role="button"
>
<i
class="CompassIcon icon-link-variant LinkIcon"
/>
<div
class="menu-name"
>
Copy link
</div>
<div
class="noicon"
/>
</div>
</div>
<div
class="menu-spacer hideOnWidescreen"
/>
<div
class="menu-options hideOnWidescreen"
>
<div
aria-label="Cancel"
class="MenuOption TextOption menu-option menu-cancel"
role="button"
>
<div
class="noicon"
/>
<div
class="menu-name"
>
Cancel
</div>
<div
class="noicon"
/>
</div>
</div>
</div>
</div>
</div>
<div
class="gallery-item"
/>
</div>
<div
class="GalleryCard"
draggable="true"
style="opacity: 1;"
>
<div
aria-label="menuwrapper"
class="MenuWrapper optionsMenu"
role="button"
>
<div
class="Button IconButton"
role="button"
>
<i
class="CompassIcon icon-dots-horizontal OptionsIcon"
/>
</div>
</div>
<div
class="gallery-item"
/>
</div>
<div
class="octo-gallery-new"
>
+ New
</div>
</div>
</div>
`;

View file

@ -0,0 +1,126 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react'
import {render, screen, fireEvent} from '@testing-library/react'
import {Provider as ReduxProvider} from 'react-redux'
import userEvent from '@testing-library/user-event'
import {mocked} from 'ts-jest/utils'
import {wrapDNDIntl, mockStateStore} from '../../testUtils'
import {TestBlockFactory} from '../../test/testBlockFactory'
import mutator from '../../mutator'
import Gallery from './gallery'
jest.mock('../../mutator')
const mockedMutator = mocked(mutator, true)
describe('src/components/gallery/Gallery', () => {
const board = TestBlockFactory.createBoard()
const activeView = TestBlockFactory.createBoardView(board)
activeView.fields.sortOptions = []
const card = TestBlockFactory.createCard(board)
const card2 = TestBlockFactory.createCard(board)
const contents = [TestBlockFactory.createDivider(card), TestBlockFactory.createDivider(card), TestBlockFactory.createDivider(card2)]
const state = {
contents,
cards: {
cards: {
[card.id]: card,
},
},
comments: {
comments: {},
},
}
const store = mockStateStore([], state)
beforeEach(() => {
jest.clearAllMocks()
})
test('should match snapshot', () => {
const {container} = render(wrapDNDIntl(
<ReduxProvider store={store}>
<Gallery
board={board}
cards={[card, card2]}
activeView={activeView}
readonly={false}
addCard={jest.fn()}
selectedCardIds={[card.id]}
onCardClicked={jest.fn()}
/>
</ReduxProvider>,
))
const buttonElement = screen.getAllByRole('button', {name: 'menuwrapper'})[0]
userEvent.click(buttonElement)
expect(container).toMatchSnapshot()
})
test('return Gallery and click new', () => {
const mockAddCard = jest.fn()
const {container} = render(wrapDNDIntl(
<ReduxProvider store={store}>
<Gallery
board={board}
cards={[card, card2]}
activeView={activeView}
readonly={false}
addCard={mockAddCard}
selectedCardIds={[card.id]}
onCardClicked={jest.fn()}
/>
</ReduxProvider>,
))
expect(container).toMatchSnapshot()
const elementNew = container.querySelector('.octo-gallery-new')!
expect(elementNew).toBeDefined()
userEvent.click(elementNew)
expect(mockAddCard).toBeCalledTimes(1)
})
test('return Gallery readonly', () => {
const {container} = render(wrapDNDIntl(
<ReduxProvider store={store}>
<Gallery
board={board}
cards={[card, card2]}
activeView={activeView}
readonly={true}
addCard={jest.fn()}
selectedCardIds={[card.id]}
onCardClicked={jest.fn()}
/>
</ReduxProvider>,
))
expect(container).toMatchSnapshot()
})
test('return Gallery and drag and drop card', async () => {
const {container} = render(wrapDNDIntl(
<ReduxProvider store={store}>
<Gallery
board={board}
cards={[card, card2]}
activeView={activeView}
readonly={false}
addCard={jest.fn()}
selectedCardIds={[]}
onCardClicked={jest.fn()}
/>
</ReduxProvider>,
))
const allGalleryCard = container.querySelectorAll('.GalleryCard')
const drag = allGalleryCard[0]
const drop = allGalleryCard[1]
fireEvent.dragStart(drag)
fireEvent.dragEnter(drop)
fireEvent.dragOver(drop)
fireEvent.drop(drop)
expect(mockedMutator.performAsUndoGroup).toBeCalledTimes(1)
})
})