Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.

Commit

Permalink
fix reseting of schedule status on successful transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
James Baxley committed Jan 9, 2017
1 parent dcf7d39 commit 23b6358
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 28 deletions.
11 changes: 9 additions & 2 deletions imports/components/@primitives/UI/forms/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export default class Checkbox extends React.Component {
checked: false,
}

componentWillReceiveProps(nextProps) {
if (this.props.defaultValue && !nextProps.defaultValue) {
this.setState({ checked: false });
}
}

disabled = () => {
if (this.props.disabled) {
return this.props.disabled; // eslint-disable-line
Expand Down Expand Up @@ -109,7 +115,7 @@ export default class Checkbox extends React.Component {
style={{ marginTop: "-1px", cursor: "pointer" }}
htmlFor={this.props.id || this.props.label || this.props.name}
>
<small data-spec="input-label" style={this.getStyles(this.props.defaultValue)}>
<small data-spec="input-label" style={this.getStyles(this.state.checked !== false)}>
{this.props.children}
</small>
</label>
Expand All @@ -120,9 +126,10 @@ export default class Checkbox extends React.Component {
name={this.props.name || this.props.label}
className={this.props.inputClasses}
disabled={this.disabled()}
defaultChecked={(this.state.checked !== false || this.props.defaultValue) ? "checked" : ""}
checked={(this.state.checked !== false || this.props.defaultValue) ? "checked" : ""}
onClick={this.props.clicked}
style={{ width: 0 }}

data-spec="input"
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ it ("should accept a default value", () => {

const inputProps = getSingleSpecWrapper(component, "input").props();

expect(inputProps.defaultChecked).toEqual("checked");
expect(inputProps.checked).toEqual("checked");
});

it ("should disable input with disabled prop", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ exports[`test pushes type prop to inputclasses 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
style={
Object {
"width": 0,
Expand Down Expand Up @@ -57,15 +57,11 @@ exports[`test should accept a default value 1`] = `
}>
<small
data-spec="input-label"
style={
Object {
"color": "#303030",
}
} />
style={Object {}} />
</label>
<input
checked="checked"
data-spec="input"
defaultChecked="checked"
style={
Object {
"width": 0,
Expand Down Expand Up @@ -100,8 +96,8 @@ exports[`test should accept an id 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
id="harambe"
style={
Object {
Expand Down Expand Up @@ -139,8 +135,8 @@ exports[`test should accept label 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
id="harambe"
name="harambe"
style={
Expand Down Expand Up @@ -179,8 +175,8 @@ exports[`test should accept name prop 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
id="harambe"
name="harambe"
style={
Expand Down Expand Up @@ -223,8 +219,8 @@ exports[`test should add classes with classes prop 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
style={
Object {
"width": 0,
Expand Down Expand Up @@ -258,8 +254,8 @@ exports[`test should add classes with classes prop 2`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
style={
Object {
"width": 0,
Expand Down Expand Up @@ -293,9 +289,9 @@ exports[`test should add inputClasses with inputClasses prop 1`] = `
style={Object {}} />
</label>
<input
checked=""
className="test1 harambe"
data-spec="input"
defaultChecked=""
style={
Object {
"width": 0,
Expand Down Expand Up @@ -329,8 +325,8 @@ exports[`test should disable input with disabled prop 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
disabled={true}
style={
Object {
Expand Down Expand Up @@ -366,8 +362,8 @@ exports[`test should display children 1`] = `
</small>
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
style={
Object {
"width": 0,
Expand Down Expand Up @@ -401,8 +397,8 @@ exports[`test should hide label with hideLabel prop 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
style={
Object {
"width": 0,
Expand Down Expand Up @@ -431,8 +427,8 @@ exports[`test should render with minimal props 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
style={
Object {
"width": 0,
Expand Down Expand Up @@ -466,8 +462,8 @@ exports[`test should respond to onclick prop 1`] = `
style={Object {}} />
</label>
<input
checked=""
data-spec="input"
defaultChecked=""
onClick={[Function]}
style={
Object {
Expand Down
8 changes: 7 additions & 1 deletion imports/components/giving/add-to-cart/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ILayout = {
authorized: boolean,
canCheckout: boolean,
setCanCheckout: Function,
bindSubComponentReset: Function,
}

export default ({
Expand All @@ -32,6 +33,7 @@ export default ({
authorized,
canCheckout,
setCanCheckout,
bindSubComponentReset,
}: ILayout) => (
<div className="push-top@handheld soft-half-top@lap-and-up">
<Forms.Form
Expand Down Expand Up @@ -74,7 +76,11 @@ export default ({
</div>

{/* Schedule */}
<ScheduleLayout setCanCheckout={setCanCheckout} authorized={authorized} />
<ScheduleLayout
setCanCheckout={setCanCheckout}
authorized={authorized}
bindSubComponentReset={bindSubComponentReset}
/>

{/* Total information */}
<h3 className="display-inline-block text-dark-primary push-half-bottom push-half-right" style={{ verticalAlign: "middle" }}>
Expand Down
16 changes: 16 additions & 0 deletions imports/components/giving/add-to-cart/Schedule/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const generateComponent = (additionalProps = {}) => {
authorized: true,
saveSchedule: jest.fn(),
setCanCheckout: jest.fn(),
bindSubComponentReset: jest.fn(),
};
return (
<Schedule {...defaultProps} {...additionalProps} />
Expand Down Expand Up @@ -284,4 +285,19 @@ describe("Class", () => {
});
});
});

describe("reset-binding", () => {
it("resets when called", () => {
let reset = null;
const bindSubComponentReset = (fn) => reset = jest.fn(fn);

const wrapper = mount(generateComponent({ bindSubComponentReset }));
const originalState = wrapper.state();
wrapper.setState({ checked: true });

reset();
expect(wrapper.state()).toEqual(originalState);
});
});

});
2 changes: 2 additions & 0 deletions imports/components/giving/add-to-cart/Schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type IScheduleProps = {
saveSchedule: Function,
setCanCheckout: Function,
preCheck: boolean,
bindSubComponentReset: Function,
};

type IScheduleState = {
Expand Down Expand Up @@ -50,6 +51,7 @@ export class Schedule extends Component {

componentWillMount() {
if (this.props.preCheck) this.setState({ checked: true });
this.props.bindSubComponentReset(this.toggleSchedule);
}

componentDidMount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ exports[`CartContainer > Lifecycle functions accepts query string prefills the f
},
]
}
bindSubComponentReset={[Function]}
canCheckout={true}
changeAmount={[Function]}
changeFund={[Function]}
Expand Down Expand Up @@ -322,6 +323,7 @@ exports[`CartContainer > Lifecycle functions accepts query string prefills the f
<div
className="display-block one-whole soft-bottom" />
<mockConstructor
bindSubComponentReset={[Function]}
setCanCheckout={[Function]}>
<div />
</mockConstructor>
Expand Down Expand Up @@ -402,7 +404,7 @@ Object {

exports[`CartContainer > Lifecycle functions should clear transactions on success 1`] = `
Object {
"canCheckout": true,
"canCheckout": false,
"subfunds": Array [
Object {
"accounts": Array [
Expand All @@ -422,7 +424,7 @@ Object {

exports[`CartContainer > Lifecycle functions should clear transactions on when amount is set to 0 1`] = `
Object {
"canCheckout": true,
"canCheckout": false,
"subfunds": Array [
Object {
"accounts": Array [
Expand Down Expand Up @@ -498,6 +500,7 @@ exports[`test should properly render with multiple accounts 1`] = `
},
]
}
bindSubComponentReset={[Function]}
canCheckout={false}
changeAmount={[Function]}
changeFund={[Function]}
Expand Down Expand Up @@ -604,6 +607,7 @@ exports[`test should properly render with multiple accounts 1`] = `
</SmallButton>
</div>
<mockConstructor
bindSubComponentReset={[Function]}
setCanCheckout={[Function]}>
<div />
</mockConstructor>
Expand Down Expand Up @@ -672,6 +676,7 @@ exports[`test should render with minimal props 1`] = `
},
]
}
bindSubComponentReset={[Function]}
canCheckout={false}
changeAmount={[Function]}
changeFund={[Function]}
Expand Down
4 changes: 4 additions & 0 deletions imports/components/giving/add-to-cart/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@ describe("CartContainer > Lifecycle functions", () => {

it("should clear transactions on success", () => {
const spy = jest.fn();
const resetSpy = jest.fn();
const component = mount(generateComponent({
clearTransactions: spy,
status: "success",
}));
const { bindSubComponentReset } = component.instance();
bindSubComponentReset(resetSpy);
expect(spy).toHaveBeenCalledTimes(1);
component.setProps({ status: "default" });
expect(component.state()).toMatchSnapshot();
expect(resetSpy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledTimes(2);
});

Expand Down
9 changes: 8 additions & 1 deletion imports/components/giving/add-to-cart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class CartContainer extends Component {
accounts: [],
}

resetCbs = []
state = { subfunds: [], canCheckout: true }

componentWillMount() {
Expand Down Expand Up @@ -89,11 +90,16 @@ class CartContainer extends Component {
(this.props.total && total === 0)
) {
this.props.clearTransactions();
this.resetCbs.forEach((fn) => fn && fn());
const subfunds = this.calculateDefaultSubfunds();
this.setState({ subfunds });
this.setState({ subfunds, canCheckout: false });
}
}

bindSubComponentReset = (reset) => {
this.resetCbs.push(reset);
}

calculateDefaultSubfunds = (query: Object = {}) => {
// copy array to mutate it
let accounts = [...this.props.accounts];
Expand Down Expand Up @@ -321,6 +327,7 @@ class CartContainer extends Component {
authorized={authorized}
canCheckout={this.canCheckout(total)}
setCanCheckout={this.setCanCheckout}
bindSubComponentReset={this.bindSubComponentReset}
/>

);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ exports[`Layout renders custom toggles if passed 1`] = `
</small>
</label>
<input
checked=""
className={undefined}
data-spec="input"
defaultChecked=""
disabled={undefined}
id="savePayment"
name="savePayment"
Expand Down Expand Up @@ -372,9 +372,9 @@ exports[`Layout renders default toggles by default 1`] = `
</small>
</label>
<input
checked=""
className={undefined}
data-spec="input"
defaultChecked=""
disabled={undefined}
id="savePayment"
name="savePayment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ exports[`test renders with props 1`] = `
</small>
</label>
<input
checked=""
className={undefined}
data-spec="input"
defaultChecked=""
disabled={undefined}
id="savePayment"
name="savePayment"
Expand Down

0 comments on commit 23b6358

Please sign in to comment.