-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1441 from Shopify/Add-account-to-standard-API
Add authenticated account to standard api
- Loading branch information
Showing
8 changed files
with
170 additions
and
44 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/ui-extensions-react/src/surfaces/customer-account/hooks/authenticated-account.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { | ||
Customer, | ||
PurchasingCompany, | ||
RenderExtensionTarget, | ||
} from '@shopify/ui-extensions/customer-account'; | ||
|
||
import {useApi} from './api'; | ||
import {useSubscription} from './subscription'; | ||
|
||
/** | ||
* Returns the current authenticated `Customer`. | ||
* | ||
* The value is `undefined` if the customer hasn't authenticated yet. | ||
*/ | ||
export function useAuthenticatedAccountCustomer< | ||
Target extends RenderExtensionTarget, | ||
>(): Customer | undefined { | ||
const account = useApi<Target>().authenticatedAccount; | ||
|
||
return useSubscription(account.customer); | ||
} | ||
|
||
/** | ||
* Provides information about the company and its location the authenticated business customer. | ||
* The value is `undefined` if a business customer isn't authenticated. | ||
*/ | ||
export function useAuthenticatedAccountPurchasingCompany< | ||
Target extends RenderExtensionTarget, | ||
>(): PurchasingCompany | undefined { | ||
const account = useApi<Target>().authenticatedAccount; | ||
|
||
return useSubscription(account.purchasingCompany); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...extensions-react/src/surfaces/customer-account/hooks/tests/authenticated-account.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {StatefulRemoteSubscribable} from '@remote-ui/async-subscription'; | ||
import {faker} from '@faker-js/faker'; | ||
import { | ||
useAuthenticatedAccountCustomer, | ||
useAuthenticatedAccountPurchasingCompany, | ||
} from '../authenticated-account'; | ||
|
||
import {mount, createMockStatefulRemoteSubscribable} from './mount'; | ||
|
||
function createMockCustomer() { | ||
return { | ||
id: `gid://shopify/Customer/${faker.datatype.number({ | ||
min: 1, | ||
precision: 1, | ||
})}`, | ||
}; | ||
} | ||
|
||
function createMockPurchasingCompany() { | ||
return { | ||
company: { | ||
id: `gid://shopify/Company/${faker.datatype.number({ | ||
min: 1, | ||
precision: 1, | ||
})}`, | ||
}, | ||
}; | ||
} | ||
|
||
function createMockHookContext(customer = {}, purchasingCompany = {}) { | ||
return { | ||
extensionApi: { | ||
authenticatedAccount: { | ||
customer: createMockStatefulRemoteSubscribable(customer), | ||
purchasingCompany: | ||
createMockStatefulRemoteSubscribable(purchasingCompany), | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
describe('account Hooks', () => { | ||
describe('useLoggedInAccountCustomer()', () => { | ||
it('returns id of the logged in customer', () => { | ||
const customer = createMockCustomer(); | ||
const {value} = mount.hook( | ||
() => useAuthenticatedAccountCustomer(), | ||
createMockHookContext(customer), | ||
); | ||
expect(customer).toBeDefined(); | ||
expect(customer.id).toBeDefined(); | ||
expect(value.id).toBe(customer.id); | ||
}); | ||
|
||
it('returns company id of which the logged in customer belongs to', () => { | ||
const purchasingCompany = createMockPurchasingCompany(); | ||
const {value} = mount.hook( | ||
() => useAuthenticatedAccountPurchasingCompany(), | ||
createMockHookContext({}, purchasingCompany), | ||
); | ||
expect(purchasingCompany).toBeDefined(); | ||
expect(purchasingCompany.company.id).toBeDefined(); | ||
expect(value.company.id).toBe(purchasingCompany.company.id); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters