-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix show multiple gift cards in order details (#4814)
* Show multiple order gift cards * Extract ThemeWrapper * Write tests * Rename to OrderUsedGiftCards, add this to order summary * Add changeset * Fix for comma * Update tests
- Loading branch information
Showing
13 changed files
with
154 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Show all gift card used in order details |
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
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
75 changes: 75 additions & 0 deletions
75
src/orders/components/OrderUsedGiftCards/OrderUsedGiftCards.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,75 @@ | ||
import { OrderDetailsFragment } from "@dashboard/graphql"; | ||
import { ThemeWrapper } from "@test/themeWrapper"; | ||
import { render, screen } from "@testing-library/react"; | ||
import React, { ReactNode } from "react"; | ||
import { IntlProvider } from "react-intl"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
|
||
import { messages } from "./messages"; | ||
import { OrderUsedGiftCards } from "./OrderUsedGiftCards"; | ||
|
||
const mockGiftCards = [ | ||
{ | ||
id: "1", | ||
last4CodeChars: "LTD2", | ||
}, | ||
{ | ||
id: "2", | ||
last4CodeChars: "FRTG", | ||
}, | ||
] as OrderDetailsFragment["giftCards"]; | ||
|
||
const Wrapper = ({ children }: { children: ReactNode }) => { | ||
return ( | ||
<BrowserRouter> | ||
<ThemeWrapper> | ||
<IntlProvider | ||
locale="EN" | ||
messages={{ | ||
paidWithGiftCard: messages.usedGiftCard.defaultMessage, | ||
}} | ||
> | ||
{children} | ||
</IntlProvider> | ||
</ThemeWrapper> | ||
</BrowserRouter> | ||
); | ||
}; | ||
|
||
describe("OrderUsedGiftCards", () => { | ||
it("should render single gift card", () => { | ||
// Arrange & Act | ||
render(<OrderUsedGiftCards giftCards={[mockGiftCards[0]]} />, { | ||
wrapper: Wrapper, | ||
}); | ||
|
||
// Assert | ||
expect(screen.getByText(/paid with gift card/i)).toBeInTheDocument(); | ||
expect(screen.getByRole("link", { name: "LTD2" })).toHaveAttribute( | ||
"href", | ||
"/gift-cards/1", | ||
); | ||
expect(screen.getByText(/LTD2/)).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render multipe gift cards", () => { | ||
// Arrange & Act | ||
render( | ||
<OrderUsedGiftCards giftCards={[mockGiftCards[0], mockGiftCards[1]]} />, | ||
{ wrapper: Wrapper }, | ||
); | ||
|
||
// Assert | ||
expect(screen.getByText(/paid with gift card/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/LTD2/)).toBeInTheDocument(); | ||
expect(screen.getByRole("link", { name: "LTD2," })).toHaveAttribute( | ||
"href", | ||
"/gift-cards/1", | ||
); | ||
expect(screen.getByText(/FRTG/)).toBeInTheDocument(); | ||
expect(screen.getByRole("link", { name: "FRTG" })).toHaveAttribute( | ||
"href", | ||
"/gift-cards/2", | ||
); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
src/orders/components/OrderUsedGiftCards/OrderUsedGiftCards.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,35 @@ | ||
import Link from "@dashboard/components/Link"; | ||
import { giftCardPath } from "@dashboard/giftCards/urls"; | ||
import { OrderDetailsFragment } from "@dashboard/graphql"; | ||
import { Box } from "@saleor/macaw-ui-next"; | ||
import React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import { messages } from "./messages"; | ||
import { showComma } from "./utils"; | ||
|
||
interface OrderUsedGiftCardsProps { | ||
giftCards: OrderDetailsFragment["giftCards"]; | ||
} | ||
|
||
export const OrderUsedGiftCards = ({ giftCards }: OrderUsedGiftCardsProps) => { | ||
return ( | ||
<FormattedMessage | ||
{...messages.usedGiftCard} | ||
values={{ | ||
link: giftCards.map(({ id, last4CodeChars }, index) => { | ||
const hasComma = showComma(giftCards.length, index); | ||
|
||
return ( | ||
<Link key={id} href={giftCardPath(id)}> | ||
<Box as="span" marginRight={hasComma ? 1 : 0}> | ||
{last4CodeChars} | ||
{hasComma && ", "} | ||
</Box> | ||
</Link> | ||
); | ||
}), | ||
}} | ||
/> | ||
); | ||
}; |
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 @@ | ||
export * from "./OrderUsedGiftCards"; |
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,9 @@ | ||
import { defineMessages } from "react-intl"; | ||
|
||
export const messages = defineMessages({ | ||
usedGiftCard: { | ||
id: "yivxZJ", | ||
defaultMessage: "Paid with Gift Card: ({link})", | ||
description: "order payment", | ||
}, | ||
}); |
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,3 @@ | ||
export const showComma = (totalLength: number, currentIndex: number) => { | ||
return totalLength - 1 !== currentIndex; | ||
}; |
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,11 @@ | ||
import React, { ReactNode } from 'react' | ||
import { ThemeProvider as LegacyThemeProvider } from "@saleor/macaw-ui"; | ||
import { ThemeProvider } from "@saleor/macaw-ui-next"; | ||
|
||
export const ThemeWrapper = ({ children }: {children: ReactNode}) => ( | ||
<LegacyThemeProvider> | ||
<ThemeProvider> | ||
{children} | ||
</ThemeProvider> | ||
</LegacyThemeProvider> | ||
) |
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