This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(composables): provide useProductAssociations for getting associa…
…tions (#1392) Co-authored-by: Patryk Tomczyk <13100280+patzick@users.noreply.github.com>
- Loading branch information
Showing
25 changed files
with
459 additions
and
82 deletions.
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
docs/landing/resources/api/composables.iuseproductassociations.isloading.md
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,16 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseProductAssociations](./composables.iuseproductassociations.md) > [isLoading](./composables.iuseproductassociations.isloading.md) | ||
|
||
## IUseProductAssociations.isLoading property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
If it's loading - indicator | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
isLoading: ComputedRef<boolean>; | ||
``` |
19 changes: 19 additions & 0 deletions
19
docs/landing/resources/api/composables.iuseproductassociations.loadassociations.md
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,19 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseProductAssociations](./composables.iuseproductassociations.md) > [loadAssociations](./composables.iuseproductassociations.loadassociations.md) | ||
|
||
## IUseProductAssociations.loadAssociations property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Start loading resources | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
loadAssociations: (params: { | ||
params: unknown; | ||
method: "post" | "get"; | ||
}) => Promise<void>; | ||
``` |
25 changes: 25 additions & 0 deletions
25
docs/landing/resources/api/composables.iuseproductassociations.md
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,25 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseProductAssociations](./composables.iuseproductassociations.md) | ||
|
||
## IUseProductAssociations interface | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
interface for [IUseProductAssociations](./composables.iuseproductassociations.md) composable | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface IUseProductAssociations | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [isLoading](./composables.iuseproductassociations.isloading.md) | ComputedRef<boolean> | <b><i>(BETA)</i></b> If it's loading - indicator | | ||
| [loadAssociations](./composables.iuseproductassociations.loadassociations.md) | (params: { params: unknown; method: "post" \| "get"; }) => Promise<void> | <b><i>(BETA)</i></b> Start loading resources | | ||
| [productAssociations](./composables.iuseproductassociations.productassociations.md) | ComputedRef<CrossSelling\[\]> | <b><i>(BETA)</i></b> | | ||
|
14 changes: 14 additions & 0 deletions
14
...anding/resources/api/composables.iuseproductassociations.productassociations.md
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,14 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [IUseProductAssociations](./composables.iuseproductassociations.md) > [productAssociations](./composables.iuseproductassociations.productassociations.md) | ||
|
||
## IUseProductAssociations.productAssociations property | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
<b>Signature:</b> | ||
|
||
```typescript | ||
productAssociations: ComputedRef<CrossSelling[]>; | ||
``` |
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
41 changes: 41 additions & 0 deletions
41
docs/landing/resources/api/composables.useproductassociations.md
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,41 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [@shopware-pwa/composables](./composables.md) > [useProductAssociations](./composables.useproductassociations.md) | ||
|
||
## useProductAssociations() function | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Get product association entity. Options - [IUseProductAssociations](./composables.iuseproductassociations.md) | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare function useProductAssociations(rootContext: ApplicationVueContext, product: Product, association: "cross-selling" | "reviews"): IUseProductAssociations; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| rootContext | [ApplicationVueContext](./composables.applicationvuecontext.md) | | | ||
| product | Product | | | ||
| association | "cross-selling" \| "reviews" | | | ||
|
||
<b>Returns:</b> | ||
|
||
[IUseProductAssociations](./composables.iuseproductassociations.md) | ||
|
||
## Example | ||
|
||
Example of possibilities: | ||
|
||
```ts | ||
const { loading, loadAssociations, productAssociations } = useProductAssociation(root, product, "cross-selling") | ||
if (!productAssociations.value) { | ||
await loadAssociations() | ||
} | ||
|
||
``` | ||
|
19 changes: 0 additions & 19 deletions
19
docs/landing/resources/api/shopware-6-client.clientsettings.auth.md
This file was deleted.
Oops, something went wrong.
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
169 changes: 169 additions & 0 deletions
169
packages/composables/__tests__/useProductAssociation.spec.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,169 @@ | ||
import Vue from "vue"; | ||
import VueCompositionApi from "@vue/composition-api"; | ||
Vue.use(VueCompositionApi); | ||
|
||
import * as Composables from "@shopware-pwa/composables"; | ||
jest.mock("@shopware-pwa/composables"); | ||
const mockedComposables = Composables as jest.Mocked<typeof Composables>; | ||
import { useProductAssociations } from "../src/logic/useProductAssociations"; | ||
import * as shopwareClient from "@shopware-pwa/shopware-6-client"; | ||
|
||
jest.mock("@shopware-pwa/shopware-6-client"); | ||
const mockedAxios = shopwareClient as jest.Mocked<typeof shopwareClient>; | ||
|
||
describe("Composables - useProductAssociations", () => { | ||
const consoleErrorSpy = jest.spyOn(console, "error"); | ||
const mockedInvokePost = jest.fn(); | ||
const mockedInvokeGet = jest.fn(); | ||
const rootContextMock: any = { | ||
$shopwareApiInstance: { | ||
defaults: { | ||
headers: {}, | ||
}, | ||
invokePost: mockedInvokePost, | ||
invokeGet: mockedInvokeGet, | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
consoleErrorSpy.mockImplementationOnce(() => {}); | ||
mockedAxios.getProductDetailsEndpoint.mockReturnValue( | ||
"/product/v4/product-id" | ||
); | ||
mockedComposables.getApplicationContext.mockImplementation(() => { | ||
return { | ||
apiInstance: rootContextMock.$shopwareApiInstance, | ||
contextName: "useProductAssociations", | ||
} as any; | ||
}); | ||
}); | ||
describe("on init", () => { | ||
it("should have empty (array) associations computed property and isLoading to false", () => { | ||
const { productAssociations, isLoading } = useProductAssociations( | ||
rootContextMock, | ||
{ id: "product-id" } as any, | ||
"cross-selling" | ||
); | ||
expect(isLoading.value).toBeFalsy(); | ||
expect(productAssociations.value).toStrictEqual([]); | ||
}); | ||
}); | ||
describe("methods", () => { | ||
describe("loadAssociations", () => { | ||
it("should catch the error on apiClient rejection", async () => { | ||
mockedAxios.invokePost.mockRejectedValueOnce( | ||
new Error("An error occured") | ||
); | ||
const { loadAssociations } = useProductAssociations( | ||
rootContextMock, | ||
{ id: "product-id" } as any, | ||
"cross-selling" | ||
); | ||
await loadAssociations(undefined as any); | ||
expect(consoleErrorSpy).toBeCalledWith( | ||
`[useProductAssociations][loadAssociations][error]:`, | ||
expect.any(Error) | ||
); | ||
}); | ||
it("should invoke GET request (default is POST) for given association within a product endpoint", async () => { | ||
mockedAxios.invokePost.mockResolvedValueOnce({ | ||
data: { | ||
associatedProducts: [], | ||
}, | ||
}); | ||
const { | ||
loadAssociations, | ||
productAssociations, | ||
} = useProductAssociations( | ||
rootContextMock, | ||
{ id: "product-id" } as any, | ||
"cross-selling" | ||
); | ||
await loadAssociations(undefined as any); | ||
expect(mockedAxios.invokePost).toBeCalledWith( | ||
{ | ||
address: "/product/v4/product-id/cross-selling", | ||
payload: undefined, | ||
}, | ||
{ | ||
defaults: { headers: {} }, | ||
invokeGet: expect.any(Function), | ||
invokePost: expect.any(Function), | ||
} | ||
); | ||
|
||
expect(productAssociations.value).toStrictEqual({ | ||
associatedProducts: [], | ||
}); | ||
}); | ||
it("should not set incoming associations if response does not match for POST", async () => { | ||
mockedAxios.invokePost.mockResolvedValueOnce(undefined); | ||
const { | ||
loadAssociations, | ||
productAssociations, | ||
} = useProductAssociations( | ||
rootContextMock, | ||
{ id: "product-id" } as any, | ||
"cross-selling" | ||
); | ||
await loadAssociations(undefined as any); | ||
expect(productAssociations.value).toStrictEqual([]); | ||
}); | ||
it("should set incoming associations if response matches for GET", async () => { | ||
mockedAxios.invokeGet.mockResolvedValueOnce({ data: 12345 }); | ||
const { | ||
loadAssociations, | ||
productAssociations, | ||
} = useProductAssociations( | ||
rootContextMock, | ||
{ id: "product-id" } as any, | ||
"cross-selling" | ||
); | ||
await loadAssociations({ method: "get" } as any); | ||
expect(productAssociations.value).toStrictEqual(12345); | ||
}); | ||
it("should invoke GET request for given association within a product endpoint", async () => { | ||
const { loadAssociations } = useProductAssociations( | ||
rootContextMock, | ||
{ id: "product-id" } as any, | ||
"cross-selling" | ||
); | ||
await loadAssociations({ method: "get" } as any); | ||
expect(mockedAxios.invokeGet).toBeCalledWith( | ||
{ | ||
address: "/product/v4/product-id/cross-selling", | ||
payload: undefined, | ||
}, | ||
{ | ||
defaults: { headers: {} }, | ||
invokeGet: expect.any(Function), | ||
invokePost: expect.any(Function), | ||
} | ||
); | ||
}); | ||
it("should invoke GET request for given association within a product endpoint - including params", async () => { | ||
const { loadAssociations } = useProductAssociations( | ||
rootContextMock, | ||
{ id: "product-id" } as any, | ||
"cross-selling" | ||
); | ||
await loadAssociations({ | ||
method: "get", | ||
params: "?someParam=true", | ||
} as any); | ||
expect(mockedAxios.invokeGet).toBeCalledWith( | ||
{ | ||
address: "/product/v4/product-id/cross-selling?someParam=true", | ||
payload: undefined, | ||
}, | ||
{ | ||
defaults: { headers: {} }, | ||
invokeGet: expect.any(Function), | ||
invokePost: expect.any(Function), | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
54f705d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: