import {render, screen} from 'sentry-test/reactTestingLibrary'; import Access from 'sentry/components/acl/access'; import ConfigStore from 'sentry/stores/configStore'; describe('Access', function () { const organization = TestStubs.Organization({ access: ['project:write', 'project:read'], }); const routerContext = TestStubs.routerContext([{organization}]); describe('as render prop', function () { const childrenMock = jest.fn().mockReturnValue(null); beforeEach(function () { childrenMock.mockClear(); }); it('has access when requireAll is false', function () { render( {childrenMock} , {context: routerContext} ); expect(childrenMock).toHaveBeenCalledWith({ hasAccess: true, hasSuperuser: false, }); }); it('has access', function () { render({childrenMock}, { context: routerContext, }); expect(childrenMock).toHaveBeenCalledWith({ hasAccess: true, hasSuperuser: false, }); }); it('has no access', function () { render({childrenMock}, { context: routerContext, }); expect(childrenMock).toHaveBeenCalledWith({ hasAccess: false, hasSuperuser: false, }); }); it('calls render function when no access', function () { const noAccessRenderer = jest.fn(() => null); render( {childrenMock} , {context: routerContext} ); expect(childrenMock).not.toHaveBeenCalled(); expect(noAccessRenderer).toHaveBeenCalled(); }); it('can specify org from props', function () { render( {childrenMock} , {context: routerContext} ); expect(childrenMock).toHaveBeenCalledWith({ hasAccess: true, hasSuperuser: false, }); }); it('handles no org/project', function () { render({childrenMock}, { context: routerContext, }); expect(childrenMock).toHaveBeenCalledWith( expect.objectContaining({ hasAccess: false, hasSuperuser: false, }) ); }); it('handles no user', function () { // Regression test for the share sheet. ConfigStore.config = { user: null, }; render({childrenMock}, {context: routerContext}); expect(childrenMock).toHaveBeenCalledWith({ hasAccess: true, hasSuperuser: false, }); }); it('is superuser', function () { ConfigStore.config = { user: {isSuperuser: true}, }; render({childrenMock}, { context: routerContext, }); expect(childrenMock).toHaveBeenCalledWith({ hasAccess: true, hasSuperuser: true, }); }); it('is not superuser', function () { ConfigStore.config = { user: {isSuperuser: false}, }; render({childrenMock}, { context: routerContext, }); expect(childrenMock).toHaveBeenCalledWith({ hasAccess: true, hasSuperuser: false, }); }); }); describe('as React node', function () { it('has access', function () { render(

The Child

, {context: routerContext} ); expect(screen.getByText('The Child')).toBeInTheDocument(); }); it('has superuser', function () { ConfigStore.config = { user: {isSuperuser: true}, }; render(

The Child

, {context: routerContext} ); expect(screen.getByText('The Child')).toBeInTheDocument(); }); it('has no access', function () { render(

The Child

, {context: routerContext} ); expect(screen.queryByText('The Child')).not.toBeInTheDocument(); }); it('has no superuser', function () { ConfigStore.config = { user: {isSuperuser: false}, }; render(

The Child

, {context: routerContext} ); expect(screen.queryByRole('The Child')).not.toBeInTheDocument(); }); }); });