Skip to content

Commit

Permalink
fix: use field instead of state, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiusj committed Sep 21, 2023
1 parent dd932d2 commit 0692df3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, {useCallback, useState} from 'react';
import React from 'react';
import {Box, Card, ChoiceList, Text, VerticalStack} from '@shopify/polaris';
import {useI18n} from '@shopify/react-i18n';

import {DiscountApplicationStrategy} from '../../types';
import {DiscountApplicationStrategy, Field} from '../../types';

export interface DiscountAppStrategyProps {
/**
* The discount application strategy.
*/
strategy: DiscountApplicationStrategy;
strategy: Field<DiscountApplicationStrategy>;
}

const I18N_SCOPE = {
Expand All @@ -17,9 +17,9 @@ const I18N_SCOPE = {

export function DiscountAppStrategyCard({strategy}: DiscountAppStrategyProps) {
const [i18n] = useI18n();
const [selected, setSelected] = useState<string[]>([strategy]);

const handleChange = useCallback((value: string[]) => setSelected(value), []);
const handleChange = (strategies: DiscountApplicationStrategy[]) =>
strategy.onChange(strategies[0]);

return (
<Box paddingBlockEnd="4">
Expand All @@ -43,7 +43,7 @@ export function DiscountAppStrategyCard({strategy}: DiscountAppStrategyProps) {
helpText: i18n.translate('maximum.helpText', I18N_SCOPE),
},
]}
selected={selected}
selected={[strategy.value]}
onChange={handleChange}
/>
</VerticalStack>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import {TextField} from '@shopify/polaris';
import {mountWithApp} from 'tests/utilities';
import {ChoiceList, TextField} from '@shopify/polaris';
import {mockField, mountWithApp} from 'tests/utilities';

import {DiscountAppStrategyCard} from '../DiscountAppStrategyCard';
import {DiscountApplicationStrategy} from '../../../types';

describe('<DiscountAppStrategyCard />', () => {
const mockProps = {
strategy: DiscountApplicationStrategy.First,
strategy: mockField(DiscountApplicationStrategy.First),
};

afterEach(() => {
Expand All @@ -21,4 +21,16 @@ describe('<DiscountAppStrategyCard />', () => {
label: 'Title',
});
});

it('calls onChange when the strategy is changed', () => {
const methodCard = mountWithApp(<DiscountAppStrategyCard {...mockProps} />);

methodCard
.find(ChoiceList)
?.trigger('onChange', [DiscountApplicationStrategy.Maximum]);

expect(mockProps.strategy.onChange).toHaveBeenCalledWith(
DiscountApplicationStrategy.Maximum,
);
});
});

0 comments on commit 0692df3

Please sign in to comment.