|
| 1 | +import React from 'react'; |
| 2 | +import { screen, render, fireEvent } from '../../../../test-utils/testing-library'; |
| 3 | +import ShareAccessSelect from '../ShareAccessSelect'; |
| 4 | +import { ACCESS_NONE, ACCESS_OPEN, ACCESS_COLLAB, ACCESS_COMPANY } from '../../../../constants'; |
| 5 | + |
| 6 | +describe('elements/common/share-access-select/ShareAccessSelect', () => { |
| 7 | + const getDefaultItem = (overrides = {}) => ({ |
| 8 | + allowed_shared_link_access_levels: [ACCESS_OPEN, ACCESS_COLLAB, ACCESS_COMPANY], |
| 9 | + permissions: { |
| 10 | + can_set_share_access: true, |
| 11 | + }, |
| 12 | + shared_link: { |
| 13 | + access: ACCESS_NONE, |
| 14 | + }, |
| 15 | + ...overrides, |
| 16 | + }); |
| 17 | + |
| 18 | + const defaultProps = { |
| 19 | + canSetShareAccess: true, |
| 20 | + className: 'test-share-access', |
| 21 | + item: getDefaultItem(), |
| 22 | + onChange: jest.fn(), |
| 23 | + }; |
| 24 | + |
| 25 | + const renderComponent = (props = {}) => { |
| 26 | + return render(<ShareAccessSelect {...defaultProps} {...props} />); |
| 27 | + }; |
| 28 | + |
| 29 | + test('should render select with all options when all access levels are allowed', () => { |
| 30 | + renderComponent(); |
| 31 | + |
| 32 | + const select = screen.getByRole('combobox'); |
| 33 | + expect(select).toHaveClass('be-share-access-select test-share-access'); |
| 34 | + |
| 35 | + const options = screen.getAllByRole('option'); |
| 36 | + expect(options).toHaveLength(4); // Open, Collab, Company, and None |
| 37 | + expect(options[0]).toHaveValue(ACCESS_OPEN); |
| 38 | + expect(screen.getByText('Access: People with the link')).toBeInTheDocument(); |
| 39 | + expect(options[1]).toHaveValue(ACCESS_COLLAB); |
| 40 | + expect(screen.getByText('Access: People in this folder')).toBeInTheDocument(); |
| 41 | + expect(options[2]).toHaveValue(ACCESS_COMPANY); |
| 42 | + expect(screen.getByText('People in this company')).toBeInTheDocument(); |
| 43 | + expect(options[3]).toHaveValue(ACCESS_NONE); |
| 44 | + expect(screen.getByText('No shared link')).toBeInTheDocument(); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should render only allowed access level options', () => { |
| 48 | + const item = getDefaultItem({ |
| 49 | + allowed_shared_link_access_levels: [ACCESS_OPEN, ACCESS_COMPANY], |
| 50 | + }); |
| 51 | + renderComponent({ item }); |
| 52 | + |
| 53 | + const options = screen.getAllByRole('option'); |
| 54 | + expect(options).toHaveLength(3); // Open, Company, and None |
| 55 | + expect(options[0]).toHaveValue(ACCESS_OPEN); |
| 56 | + expect(screen.getByText('Access: People with the link')).toBeInTheDocument(); |
| 57 | + expect(options[1]).toHaveValue(ACCESS_COMPANY); |
| 58 | + expect(screen.getByText('People in this company')).toBeInTheDocument(); |
| 59 | + expect(options[2]).toHaveValue(ACCESS_NONE); |
| 60 | + expect(screen.getByText('No shared link')).toBeInTheDocument(); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should not render select when canSetShareAccess is false', () => { |
| 64 | + renderComponent({ canSetShareAccess: false }); |
| 65 | + expect(screen.queryByRole('combobox')).not.toBeInTheDocument(); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should not render select when can_set_share_access permission is false', () => { |
| 69 | + const item = getDefaultItem({ |
| 70 | + permissions: { can_set_share_access: false }, |
| 71 | + }); |
| 72 | + renderComponent({ item }); |
| 73 | + expect(screen.queryByRole('combobox')).not.toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('should not render select when no allowed_shared_link_access_levels', () => { |
| 77 | + const item = getDefaultItem({ |
| 78 | + allowed_shared_link_access_levels: null, |
| 79 | + }); |
| 80 | + renderComponent({ item }); |
| 81 | + expect(screen.queryByRole('combobox')).not.toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + test('should call onChange with selected value and item when selection changes', () => { |
| 85 | + const onChange = jest.fn(); |
| 86 | + renderComponent({ onChange }); |
| 87 | + |
| 88 | + const select = screen.getByRole('combobox'); |
| 89 | + fireEvent.change(select, { target: { value: ACCESS_OPEN } }); |
| 90 | + |
| 91 | + expect(onChange).toHaveBeenCalledWith(ACCESS_OPEN, defaultProps.item); |
| 92 | + }); |
| 93 | + |
| 94 | + test('should show "Remove" text for ACCESS_NONE option when current access is not ACCESS_NONE', () => { |
| 95 | + const item = getDefaultItem({ |
| 96 | + shared_link: { access: ACCESS_OPEN }, |
| 97 | + }); |
| 98 | + renderComponent({ item }); |
| 99 | + |
| 100 | + const noneOption = screen.getByRole('option', { name: 'Remove shared link' }); |
| 101 | + expect(noneOption).toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + test('should show "None" text for ACCESS_NONE option when current access is ACCESS_NONE', () => { |
| 105 | + renderComponent(); |
| 106 | + |
| 107 | + const noneOption = screen.getByRole('option', { name: 'No shared link' }); |
| 108 | + expect(noneOption).toBeInTheDocument(); |
| 109 | + }); |
| 110 | +}); |
0 commit comments