@@ -30,12 +30,14 @@ import {AppAccessSpecIdentifier} from '../../models/extensions/specifications/ap
3030import { MinimalAppIdentifiers } from '../../models/organization.js'
3131import { CreateAssetUrl } from '../../api/graphql/app-management/generated/create-asset-url.js'
3232import { SourceExtension } from '../../api/graphql/app-management/generated/types.js'
33+ import { ListOrganizations } from '../../api/graphql/business-platform-destinations/generated/organizations.js'
3334import { describe , expect , test , vi } from 'vitest'
3435import { CLI_KIT_VERSION } from '@shopify/cli-kit/common/version'
3536import { fetch } from '@shopify/cli-kit/node/http'
3637import {
3738 businessPlatformOrganizationsRequest ,
3839 businessPlatformOrganizationsRequestDoc ,
40+ businessPlatformRequestDoc ,
3941} from '@shopify/cli-kit/node/api/business-platform'
4042import { appManagementRequestDoc } from '@shopify/cli-kit/node/api/app-management'
4143import { BugError } from '@shopify/cli-kit/node/error'
@@ -1528,3 +1530,124 @@ describe('appExtensionRegistrations', () => {
15281530 } )
15291531 } )
15301532} )
1533+
1534+ describe ( 'organizations' , ( ) => {
1535+ test ( 'returns empty array when currentUserAccount is null' , async ( ) => {
1536+ // Given
1537+ const client = new AppManagementClient ( )
1538+ client . businessPlatformToken = ( ) => Promise . resolve ( 'business-platform-token' )
1539+
1540+ vi . mocked ( businessPlatformRequestDoc ) . mockResolvedValueOnce ( {
1541+ currentUserAccount : null ,
1542+ } )
1543+
1544+ // When
1545+ const result = await client . organizations ( )
1546+
1547+ // Then
1548+ expect ( result ) . toEqual ( [ ] )
1549+ expect ( businessPlatformRequestDoc ) . toHaveBeenCalledWith (
1550+ expect . objectContaining ( {
1551+ query : ListOrganizations ,
1552+ token : 'business-platform-token' ,
1553+ } ) ,
1554+ )
1555+ } )
1556+
1557+ test ( 'returns organizations with unique names' , async ( ) => {
1558+ // Given
1559+ const client = new AppManagementClient ( )
1560+ client . businessPlatformToken = ( ) => Promise . resolve ( 'business-platform-token' )
1561+ const mockResponse = {
1562+ currentUserAccount : {
1563+ uuid : 'user-123' ,
1564+ organizationsWithAccessToDestination : {
1565+ nodes : [
1566+ { id : 'Z2lkOi8vQnVzaW5lc3NQbGF0Zm9ybS9Pcmdhbml6YXRpb24vMQ==' , name : 'Org One' } ,
1567+ { id : 'Z2lkOi8vQnVzaW5lc3NQbGF0Zm9ybS9Pcmdhbml6YXRpb24vMg==' , name : 'Org Two' } ,
1568+ { id : 'Z2lkOi8vQnVzaW5lc3NQbGF0Zm9ybS9Pcmdhbml6YXRpb24vMw==' , name : 'Org Three' } ,
1569+ ] ,
1570+ } ,
1571+ } ,
1572+ }
1573+ vi . mocked ( businessPlatformRequestDoc ) . mockResolvedValueOnce ( mockResponse )
1574+
1575+ // When
1576+ const result = await client . organizations ( )
1577+
1578+ // Then
1579+ expect ( result ) . toEqual ( [
1580+ { id : '1' , businessName : 'Org One' , source : 'BusinessPlatform' } ,
1581+ { id : '2' , businessName : 'Org Two' , source : 'BusinessPlatform' } ,
1582+ { id : '3' , businessName : 'Org Three' , source : 'BusinessPlatform' } ,
1583+ ] )
1584+ } )
1585+
1586+ test ( 'appends ID to businessName when organizations have duplicate names' , async ( ) => {
1587+ // Given
1588+ const client = new AppManagementClient ( )
1589+ client . businessPlatformToken = ( ) => Promise . resolve ( 'business-platform-token' )
1590+ const mockResponse = {
1591+ currentUserAccount : {
1592+ uuid : 'user-123' ,
1593+ organizationsWithAccessToDestination : {
1594+ nodes : [
1595+ { id : 'Z2lkOi8vQnVzaW5lc3NQbGF0Zm9ybS9Pcmdhbml6YXRpb24vMQ==' , name : 'My Org' } ,
1596+ { id : 'Z2lkOi8vQnVzaW5lc3NQbGF0Zm9ybS9Pcmdhbml6YXRpb24vMg==' , name : 'My Org' } ,
1597+ { id : 'Z2lkOi8vQnVzaW5lc3NQbGF0Zm9ybS9Pcmdhbml6YXRpb24vMw==' , name : 'Other Org' } ,
1598+ { id : 'Z2lkOi8vQnVzaW5lc3NQbGF0Zm9ybS9Pcmdhbml6YXRpb24vNA==' , name : 'My Org' } ,
1599+ ] ,
1600+ } ,
1601+ } ,
1602+ }
1603+ vi . mocked ( businessPlatformRequestDoc ) . mockResolvedValueOnce ( mockResponse )
1604+
1605+ // When
1606+ const result = await client . organizations ( )
1607+
1608+ // Then
1609+ expect ( result ) . toEqual ( [
1610+ {
1611+ id : '1' ,
1612+ businessName : 'My Org (1)' ,
1613+ source : 'BusinessPlatform' ,
1614+ } ,
1615+ {
1616+ id : '2' ,
1617+ businessName : 'My Org (2)' ,
1618+ source : 'BusinessPlatform' ,
1619+ } ,
1620+ {
1621+ id : '3' ,
1622+ businessName : 'Other Org (3)' ,
1623+ source : 'BusinessPlatform' ,
1624+ } ,
1625+ {
1626+ id : '4' ,
1627+ businessName : 'My Org (4)' ,
1628+ source : 'BusinessPlatform' ,
1629+ } ,
1630+ ] )
1631+ } )
1632+
1633+ test ( 'returns empty array when organizationsWithAccessToDestination is empty' , async ( ) => {
1634+ // Given
1635+ const client = new AppManagementClient ( )
1636+ client . businessPlatformToken = ( ) => Promise . resolve ( 'business-platform-token' )
1637+ const mockResponse = {
1638+ currentUserAccount : {
1639+ uuid : 'user-123' ,
1640+ organizationsWithAccessToDestination : {
1641+ nodes : [ ] ,
1642+ } ,
1643+ } ,
1644+ }
1645+ vi . mocked ( businessPlatformRequestDoc ) . mockResolvedValueOnce ( mockResponse )
1646+
1647+ // When
1648+ const result = await client . organizations ( )
1649+
1650+ // Then
1651+ expect ( result ) . toEqual ( [ ] )
1652+ } )
1653+ } )
0 commit comments