Skip to content

Commit a209790

Browse files
committed
coupon
1 parent 164d8b7 commit a209790

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/components/CouponCode.vue

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<template>
2+
<div>
3+
<input type="text" class="coupon-code" v-model="code" @input="validate">
4+
<p v-text="feedback"></p>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
data () {
11+
return {
12+
code: '',
13+
coupons: [],
14+
valid: false
15+
}
16+
},
17+
18+
computed: {
19+
selectedCoupon() {
20+
return this.coupons.find(coupon => coupon.code == this.code);
21+
},
22+
23+
message() {
24+
return this.selectedCoupon.message;
25+
},
26+
27+
feedback () {
28+
if (this.valid) {
29+
return `Coupon Redeemed: ${this.message}`;
30+
}
31+
32+
return 'Invalid Coupon Code';
33+
}
34+
},
35+
36+
methods: {
37+
validate () {
38+
this.valid = !! this.selectedCoupon;
39+
40+
if (this.valid) {
41+
this.$emit('applied', this.selectedCoupon.discount);
42+
}
43+
}
44+
}
45+
}
46+
</script>

test/couponCode.spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {mount} from "@vue/test-utils";
2+
import expect from 'expect';
3+
import CouponCode from "../src/components/CouponCode";
4+
5+
describe('CouponCode', () => {
6+
let wrapper;
7+
8+
beforeEach(() => {
9+
wrapper = mount(CouponCode);
10+
11+
wrapper.setData({
12+
coupons: [
13+
{
14+
code: '50OFF',
15+
message: '50% Off!',
16+
discount: 50
17+
}
18+
]
19+
});
20+
});
21+
22+
it('accepts the coupon code', () => {
23+
expect(wrapper.contains('input.coupon-code')).toBe(true);
24+
});
25+
26+
it('validates a real provided coupon code', () => {
27+
enterCouponCode('50OFF');
28+
29+
expect(wrapper.html()).toContain('Coupon Redeemed: 50% Off!');
30+
});
31+
32+
it('validates a fake provided coupon code', () => {
33+
enterCouponCode('NOTREAL');
34+
35+
expect(wrapper.html()).toContain('Invalid Coupon Code');
36+
});
37+
38+
it('broadcasts the percentage when a valid coupon code is applied', () => {
39+
enterCouponCode('50OFF');
40+
41+
expect(wrapper.emitted().applied).toBeTruthy();
42+
expect(wrapper.emitted().applied[0]).toEqual([50]);
43+
});
44+
45+
function enterCouponCode(code) {
46+
let couponCode = wrapper.find('input.coupon-code');
47+
48+
couponCode.element.value = code;
49+
couponCode.trigger('input');
50+
}
51+
52+
});

0 commit comments

Comments
 (0)