|
| 1 | +import React from 'react'; |
| 2 | +import _ from 'lodash'; |
| 3 | + |
| 4 | +import { render } from '../../utils/test-react-testing-library'; |
| 5 | +import { DetailContext, DetailContextProps } from '../../pages/Version'; |
| 6 | + |
| 7 | +import DetailSidebarFundButton from './DetailSidebarFundButton'; |
| 8 | + |
| 9 | +const ComponentToBeRendered: React.FC<{ contextValue: DetailContextProps }> = ({ contextValue }) => ( |
| 10 | + <DetailContext.Provider value={contextValue}> |
| 11 | + <DetailSidebarFundButton /> |
| 12 | + </DetailContext.Provider> |
| 13 | +); |
| 14 | + |
| 15 | +const detailContextValue: DetailContextProps = { |
| 16 | + packageName: 'foo', |
| 17 | + readMe: 'test', |
| 18 | + enableLoading: () => {}, |
| 19 | + isLoading: false, |
| 20 | + hasNotBeenFound: false, |
| 21 | + packageMeta: { |
| 22 | + _uplinks: {}, |
| 23 | + latest: { |
| 24 | + name: '@verdaccio/local-storage', |
| 25 | + version: '8.0.1-next.1', |
| 26 | + dist: { fileCount: 0, unpackedSize: 0, tarball: 'http://localhost:8080/bootstrap/-/bootstrap-4.3.1.tgz' }, |
| 27 | + homepage: 'https://verdaccio.org', |
| 28 | + bugs: { |
| 29 | + url: 'https://github.com/verdaccio/monorepo/issues', |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +describe('test DetailSidebarFundButton', () => { |
| 36 | + test('should not display the button if fund is missing', () => { |
| 37 | + const wrapper = render(<ComponentToBeRendered contextValue={detailContextValue} />); |
| 38 | + |
| 39 | + expect(wrapper.queryByText('Fund')).toBeNull(); |
| 40 | + }); |
| 41 | + |
| 42 | + test('should not display the button if url is missing', () => { |
| 43 | + const value = _.merge(detailContextValue, { |
| 44 | + packageMeta: { |
| 45 | + latest: { |
| 46 | + funding: {}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const wrapper = render(<ComponentToBeRendered contextValue={value} />); |
| 52 | + |
| 53 | + expect(wrapper.queryByText('Fund')).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should not display the button if url is not a string', () => { |
| 57 | + const value = _.merge(detailContextValue, { |
| 58 | + packageMeta: { |
| 59 | + latest: { |
| 60 | + funding: { |
| 61 | + url: null, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const wrapper = render(<ComponentToBeRendered contextValue={value} />); |
| 68 | + |
| 69 | + expect(wrapper.queryByText('Fund')).toBeNull(); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should not display the button if url is not an url', () => { |
| 73 | + const value = _.merge(detailContextValue, { |
| 74 | + packageMeta: { |
| 75 | + latest: { |
| 76 | + funding: { |
| 77 | + url: 'somethign different as url', |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const wrapper = render(<ComponentToBeRendered contextValue={value} />); |
| 84 | + |
| 85 | + expect(wrapper.queryByText('Fund')).toBeNull(); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should display the button if url is a valid url', () => { |
| 89 | + const value = _.merge(detailContextValue, { |
| 90 | + packageMeta: { |
| 91 | + latest: { |
| 92 | + funding: { |
| 93 | + url: 'https://opencollective.com/verdaccio', |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + const wrapper = render(<ComponentToBeRendered contextValue={value} />); |
| 100 | + |
| 101 | + expect(wrapper.getByText('Fund')).toBeTruthy(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments