|
| 1 | +import {fireEvent, within} from '@testing-library/dom'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import React from 'react'; |
| 4 | +import {Classes} from '@blueprintjs/select'; |
| 5 | +import {act, render, StoreCreator, screen} from '../../../../../test-utils'; |
| 6 | +import Content from '../../index'; |
| 7 | + |
| 8 | +//const path = creator.createPath('/user/abc'); |
| 9 | +//stores.uiStore.setActiveNode(path); |
| 10 | +//const {container} = render(<Content />, {providerProps: {value: stores}}); |
| 11 | + |
| 12 | +describe('Tags tests', () => { |
| 13 | + const getTagBtn = () => screen.getByRole('button', {name: /tags/}); |
| 14 | + |
| 15 | + const getPopover = () => { |
| 16 | + return document.querySelector(`.tagpopover`); |
| 17 | + }; |
| 18 | + |
| 19 | + const getTagsInsideSelect = () => { |
| 20 | + return getPopover().querySelectorAll('.bp3-tag'); |
| 21 | + }; |
| 22 | + |
| 23 | + const getNoResultDropdown = () => { |
| 24 | + return document.querySelector('.tagsuggest-noresult'); |
| 25 | + }; |
| 26 | + |
| 27 | + const getSuggestionsPopup = () => { |
| 28 | + return document.querySelector(`.${Classes.MULTISELECT_POPOVER}`); |
| 29 | + }; |
| 30 | + |
| 31 | + it('can add global tags', async () => { |
| 32 | + const {stores} = StoreCreator(); |
| 33 | + render(<Content />, {providerProps: {value: stores}}); |
| 34 | + expect(getTagBtn()).toBeInTheDocument(); |
| 35 | + |
| 36 | + userEvent.click(getTagBtn()); |
| 37 | + expect(getTagsInsideSelect()).toHaveLength(0); |
| 38 | + |
| 39 | + // Add a tag |
| 40 | + await userEvent.type(within(getPopover()).getByRole(/textbox/), 'cxtag', { |
| 41 | + delay: 1, |
| 42 | + }); |
| 43 | + userEvent.keyboard('{Enter}'); |
| 44 | + |
| 45 | + expect(getTagsInsideSelect()).toHaveLength(1); |
| 46 | + expect(getTagsInsideSelect()[0]).toHaveTextContent(/cxtag/); |
| 47 | + expect(getNoResultDropdown()).not.toBeInTheDocument(); |
| 48 | + |
| 49 | + // Tags are also added to suggestions |
| 50 | + expect(within(getSuggestionsPopup()).getAllByRole(/listitem/)).toHaveLength( |
| 51 | + 1, |
| 52 | + ); |
| 53 | + expect( |
| 54 | + within(getSuggestionsPopup()).getAllByRole(/listitem/)[0], |
| 55 | + ).toHaveTextContent(/cxtag/); |
| 56 | + }); |
| 57 | + |
| 58 | + it('has no suggestions when no global tags are present', async () => { |
| 59 | + const {stores} = StoreCreator(); |
| 60 | + |
| 61 | + render(<Content />, {providerProps: {value: stores}}); |
| 62 | + expect(getTagBtn()).toBeInTheDocument(); |
| 63 | + |
| 64 | + userEvent.click(getTagBtn()); |
| 65 | + expect(getTagsInsideSelect()).toHaveLength(0); |
| 66 | + |
| 67 | + // Check list dropdown |
| 68 | + await act(async () => { |
| 69 | + await userEvent.click(within(getPopover()).getByRole(/textbox/)); |
| 70 | + }); |
| 71 | + |
| 72 | + expect(getTagsInsideSelect()).toHaveLength(0); |
| 73 | + expect(getNoResultDropdown()).toBeInTheDocument(); |
| 74 | + expect(getNoResultDropdown()).toHaveTextContent(/No results./); |
| 75 | + }); |
| 76 | + |
| 77 | + it('shows global tags suggestions in path node', async () => { |
| 78 | + const {stores, creator} = StoreCreator(); |
| 79 | + |
| 80 | + const {rerender} = render(<Content />, {providerProps: {value: stores}}); |
| 81 | + // Add a tag |
| 82 | + userEvent.click(getTagBtn()); |
| 83 | + await userEvent.type(within(getPopover()).getByRole(/textbox/), 'cxtag', { |
| 84 | + delay: 1, |
| 85 | + }); |
| 86 | + userEvent.keyboard('{Enter}'); |
| 87 | + |
| 88 | + const path = creator.createPath('/user/abc'); |
| 89 | + stores.uiStore.setActiveNode(path); |
| 90 | + |
| 91 | + rerender(<Content />, {providerProps: {value: stores}}); |
| 92 | + userEvent.click(getTagBtn()); |
| 93 | + expect(getTagsInsideSelect()).toHaveLength(0); |
| 94 | + expect(within(getSuggestionsPopup()).getAllByRole(/listitem/)).toHaveLength( |
| 95 | + 1, |
| 96 | + ); |
| 97 | + expect( |
| 98 | + within(getSuggestionsPopup()).getAllByRole(/listitem/)[0], |
| 99 | + ).toHaveTextContent(/cxtag/); |
| 100 | + }); |
| 101 | +}); |
0 commit comments