11import { act , render , screen , userEvent } from 'sentry-test/reactTestingLibrary' ;
22
33import { SwitchOrganization } from 'sentry/components/sidebar/sidebarDropdown/switchOrganization' ;
4+ import { Organization } from 'sentry/types' ;
5+ import { OrganizationContext } from 'sentry/views/organizationContext' ;
46
57describe ( 'SwitchOrganization' , function ( ) {
8+ function mountWithOrg ( children , organization ?: Organization ) {
9+ if ( ! organization ) {
10+ organization = TestStubs . Organization ( ) as Organization ;
11+ }
12+ return (
13+ < OrganizationContext . Provider value = { organization } >
14+ { children }
15+ </ OrganizationContext . Provider >
16+ ) ;
17+ }
18+
619 it ( 'can list organizations' , function ( ) {
720 jest . useFakeTimers ( ) ;
821 render (
9- < SwitchOrganization
10- canCreateOrganization = { false }
11- organizations = { [
12- TestStubs . Organization ( { name : 'Organization 1' } ) ,
13- TestStubs . Organization ( { name : 'Organization 2' , slug : 'org2' } ) ,
14- ] }
15- />
22+ mountWithOrg (
23+ < SwitchOrganization
24+ canCreateOrganization = { false }
25+ organizations = { [
26+ TestStubs . Organization ( { name : 'Organization 1' } ) ,
27+ TestStubs . Organization ( { name : 'Organization 2' , slug : 'org2' } ) ,
28+ ] }
29+ />
30+ )
1631 ) ;
1732
1833 userEvent . hover ( screen . getByTestId ( 'sidebar-switch-org' ) ) ;
@@ -27,18 +42,26 @@ describe('SwitchOrganization', function () {
2742
2843 it ( 'shows "Create an Org" if they have permission' , function ( ) {
2944 jest . useFakeTimers ( ) ;
30- render ( < SwitchOrganization canCreateOrganization organizations = { [ ] } /> ) ;
45+ render ( mountWithOrg ( < SwitchOrganization canCreateOrganization organizations = { [ ] } /> ) ) ;
3146
3247 userEvent . hover ( screen . getByTestId ( 'sidebar-switch-org' ) ) ;
3348 act ( ( ) => void jest . advanceTimersByTime ( 500 ) ) ;
3449
3550 expect ( screen . getByTestId ( 'sidebar-create-org' ) ) . toBeInTheDocument ( ) ;
51+
52+ const createOrgLink = screen . getByRole ( 'link' , { name : 'Create a new organization' } ) ;
53+ expect ( createOrgLink ) . toBeInTheDocument ( ) ;
54+ expect ( createOrgLink ) . toHaveAttribute ( 'href' , '/organizations/new/' ) ;
3655 jest . useRealTimers ( ) ;
3756 } ) ;
3857
3958 it ( 'does not have "Create an Org" if they do not have permission' , function ( ) {
4059 jest . useFakeTimers ( ) ;
41- render ( < SwitchOrganization canCreateOrganization = { false } organizations = { [ ] } /> ) ;
60+ render (
61+ mountWithOrg (
62+ < SwitchOrganization canCreateOrganization = { false } organizations = { [ ] } />
63+ )
64+ ) ;
4265
4366 userEvent . hover ( screen . getByTestId ( 'sidebar-switch-org' ) ) ;
4467 act ( ( ) => void jest . advanceTimersByTime ( 500 ) ) ;
@@ -47,6 +70,36 @@ describe('SwitchOrganization', function () {
4770 jest . useRealTimers ( ) ;
4871 } ) ;
4972
73+ it ( 'uses sentry URL for "Create an Org"' , function ( ) {
74+ const currentOrg = TestStubs . Organization ( {
75+ name : 'Organization' ,
76+ slug : 'org' ,
77+ links : {
78+ organizationUrl : 'http://org.sentry.io' ,
79+ regionUrl : 'http://eu.sentry.io' ,
80+ } ,
81+ features : [ 'customer-domains' ] ,
82+ } ) ;
83+
84+ jest . useFakeTimers ( ) ;
85+ render (
86+ mountWithOrg (
87+ < SwitchOrganization canCreateOrganization organizations = { [ ] } /> ,
88+ currentOrg
89+ )
90+ ) ;
91+
92+ userEvent . hover ( screen . getByTestId ( 'sidebar-switch-org' ) ) ;
93+ act ( ( ) => void jest . advanceTimersByTime ( 500 ) ) ;
94+
95+ expect ( screen . getByTestId ( 'sidebar-create-org' ) ) . toBeInTheDocument ( ) ;
96+
97+ const createOrgLink = screen . getByRole ( 'link' , { name : 'Create a new organization' } ) ;
98+ expect ( createOrgLink ) . toBeInTheDocument ( ) ;
99+ expect ( createOrgLink ) . toHaveAttribute ( 'href' , 'https://sentry.io/organizations/new/' ) ;
100+ jest . useRealTimers ( ) ;
101+ } ) ;
102+
50103 it ( 'shows orgs pending deletion with a special icon' , function ( ) {
51104 const orgPendingDeletion = TestStubs . Organization ( {
52105 slug : 'org-2' ,
@@ -55,10 +108,12 @@ describe('SwitchOrganization', function () {
55108
56109 jest . useFakeTimers ( ) ;
57110 render (
58- < SwitchOrganization
59- canCreateOrganization
60- organizations = { [ TestStubs . Organization ( ) , orgPendingDeletion ] }
61- />
111+ mountWithOrg (
112+ < SwitchOrganization
113+ canCreateOrganization
114+ organizations = { [ TestStubs . Organization ( ) , orgPendingDeletion ] }
115+ />
116+ )
62117 ) ;
63118
64119 userEvent . hover ( screen . getByTestId ( 'sidebar-switch-org' ) ) ;
0 commit comments