0

I am trying to assert that currencyIsoCode: 'USD' is in an HTTP request body. I am able to assert that method: 'POST' is in the request body, so I am assuming the problem has something to do with the nested object. I've already tried expect.objectContaining({currencyIsocode: 'USD'}), but this also does not work due to nesting I believe. If you have any suggestions, let me know. If you have another approach other than using objectContaining(), that would be great as well.

it('should pass currencyIsoCode if currencyIsoCode is present in context', async () => {
            (<jest.Mock>getInternalContext).mockResolvedValue({ currencyIsoCode: 'USD' });
            mockAccountId = '005000000000ABC';
            const mockFetch = <jest.Mock>fetch;
            mockFetch.mockResolvedValueOnce(mockProductPromotionalPricingCollectionResponseOk());
            const result = await readPromotionalPricingCollection(productData);
            expect(result).toEqual(getPromotionalPricingCollectionData());
            expect(mockFetch).toHaveBeenCalledTimes(1);
            expect(mockFetch).toHaveBeenCalledWith(
                `/services/data/${API_VERSION}/commerce/promotions/actions/evaluate-products`,
                expect.objectContaining({
                    body: expect.objectContaining({ currencyIsoCode: 'USD', }),
                })
            );
        });

Error:

Expected: "/services/data/v58.0/commerce/promotions/actions/evaluate-products", ObjectContaining {"body": ObjectContaining {"currencyIsoCode": "USD"}}
Received: "/services/data/v58.0/commerce/promotions/actions/evaluate-products", {"body": "{\"webstoreId\":\"0ZER00000004ZWc\",\"products\":[{\"productId\":\"prod2\",\"salesPrice\":\"80.00\"}],\"currencyIsoCode\":\"USD\"}", "credentials": "same-origin", "headers": {"Content-Type": "application/json;charset=UTF-8"}, "method": "POST", "signal": {}}

1 Answer 1

0

I had to Jsonify the search query:

it('should pass currencyIsoCode if currencyIsoCode is present in context', async () => {
            (<jest.Mock>getInternalContext).mockResolvedValue({ currencyIsoCode: 'USD' });
            mockAccountId = '005000000000ABC';
            const searchQuery = {
                webstoreId: '0ZER00000004ZWc',
                products: [{ productId: 'prod1', salesPrice: '60.00' }],
                currencyIsoCode: 'USD',
            };
            const mockFetch = <jest.Mock>fetch;
            mockFetch.mockResolvedValueOnce(mockProductPromotionalPricingCollectionResponseOk());
            const result = await readPromotionalPricingCollection(productData);
            expect(result).toEqual(getPromotionalPricingCollectionData());
            expect(mockFetch).toHaveBeenCalledTimes(1);
            expect(mockFetch).toHaveBeenCalledWith(
                `/services/data/${API_VERSION}/commerce/promotions/actions/evaluate-products`,
                expect.objectContaining({
                    body: JSON.stringify(searchQuery),
                })
            );
        });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .