Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue3 #157

Merged
merged 6 commits into from
May 6, 2022
Merged

Vue3 #157

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/ContentModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default {
beforeUnmount() {
this.contentObserver.disconnect();
},
emits: ['close'],
methods: {
handleClose() {
this.$emit('close');
Expand Down
1 change: 1 addition & 0 deletions src/components/ContinueBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default {
default: true,
},
},
emits: ['continue', 'secondary'],
methods: {
onContinue() {
this.$emit('continue');
Expand Down
19 changes: 10 additions & 9 deletions src/components/CurrencyInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default {
cypressMixin,
],
props: {
value: {
modelValue: {
type: String,
validator: (value) => {
return isValidInput(value);
Expand Down Expand Up @@ -111,7 +111,7 @@ export default {
}
},
created() {
this.formattedValue = convertNumberToFormattedString(this.value);
this.formattedValue = convertNumberToFormattedString(this.modelValue);
this.inputValue = this.formattedValue;
},
mounted() {
Expand All @@ -121,7 +121,7 @@ export default {
this.$refs.input.removeEventListener('paste', this.handlePaste);
},
watch: {
value(newValue) {
modelValue(newValue) {
if (this.isEditing) {
this.inputValue = newValue || null;
} else {
Expand All @@ -130,13 +130,14 @@ export default {
}
}
},
emits: ['update:modelValue', 'input', 'blur'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please break these down with each on its own line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just left a comment that I had these changes from before the linting rules, didn't run the linter on them to keep the diff small, I was thinking we could bring this in and then run the linter on all files in one pull request without logic changes to make it easier. But could just lint too in this pr if preferable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that here though, the last commit is just linting rules, the component commits show the logic changes alone

methods: {
handleFocus() {
this.isEditing = true;
this.inputValue = this.value;
this.inputValue = this.modelValue;
},
handleBlur(event) {
let value = this.value;
let value = this.modelValue;

this.isEditing = false;
value = this.removeLeadingZeros(value);
Expand All @@ -153,15 +154,15 @@ export default {
},
handleInput(event) {
const value = event.target.value;

if (isValidInput(value)) {
this.inputValue = value;
this.formattedValue = convertNumberToFormattedString(value);
this.$emit('input', value);
this.$emit('update:modelValue', value);
} else {
// Reset input value to previous value.
this.inputValue = this.value;
this.formattedValue = convertNumberToFormattedString(this.value);
this.inputValue = this.modelValue;
this.formattedValue = convertNumberToFormattedString(this.modelValue);
}

// Prevent input focus loss during rerender.
Expand All @@ -173,7 +174,7 @@ export default {
const keyCode = event.which ? event.which : event.keyCode;

if ((keyCode >= 48 && keyCode <= 57) // Number key.
|| (keyCode === 46 && this.isCentsEnabled && !this.containsDecimal(this.value)) // Decimal key.
|| (keyCode === 46 && this.isCentsEnabled && !this.containsDecimal(this.modelValue)) // Decimal key.
|| keyCode === 45) // Minus key
{
return true;
Expand Down
6 changes: 3 additions & 3 deletions stories/components/ContentModal.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export default {
argTypes: {},
};

const Template = (args, { argTypes }) => ({
props: Object.keys(argTypes),
const Template = (args) => ({
components: {
ContentModal,
Button,
Expand All @@ -18,6 +17,7 @@ const Template = (args, { argTypes }) => ({
count: 0,
isCountRendered: true,
}),
setup() { return { args }; },
created() {
setInterval(() => {
// Used to trigger a DOM change manually.
Expand All @@ -40,7 +40,7 @@ const Template = (args, { argTypes }) => ({
<div>
<Button label="Open Modal" @click="handleClickOpenModal()" />
<ContentModal v-if="isModalShown"
v-bind="$props"
v-bind="args"
@close="handleClose()">
<p>Hello World!</p>
<p>This is a link to Google: <a href="https://google.ca" target="_blank">click here</a></p>
Expand Down
6 changes: 3 additions & 3 deletions stories/components/ContinueBar.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default {
argTypes: {},
};

const Template = (args, { argTypes }) => ({
props: Object.keys(argTypes),
const Template = (args) => ({
setup() { return { args }; },
components: { ContinueBar },
template: '<ContinueBar v-bind="$props" />',
template: '<ContinueBar v-bind="args" />',
});

export const Example = Template.bind({});
4 changes: 2 additions & 2 deletions stories/components/CurrencyInput.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export default {
};

const Template = (args, { argTypes }) => ({
props: Object.keys(argTypes).filter((x) => x !== 'value'),
setup() { return { args }; },
components: { CurrencyInput },
data: () => ({
value: null,
}),
template: '<CurrencyInput v-bind="$props" v-model="value" />',
template: '<CurrencyInput v-bind="args" v-model="value" />',
});

export const Example = Template.bind({});
Expand Down
30 changes: 26 additions & 4 deletions tests/unit/components/CurrencyInput.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { mount } from '@vue/test-utils';
import Component from '../../../src/components/CurrencyInput.vue';
import CurrencyInput from '../../../src/components/CurrencyInput.vue';

describe('CurrencyInput.vue', () => {
it('renders', () => {
const wrapper = mount(Component);
const wrapper = mount(CurrencyInput);
expect(wrapper.element).toBeDefined();
});

it('removeLeadingZeros() removes leading zeros', () => {
const wrapper = mount(Component);
const wrapper = mount(CurrencyInput);
expect(wrapper.vm.removeLeadingZeros('0')).toBe('0');
expect(wrapper.vm.removeLeadingZeros('00')).toBe('0');
expect(wrapper.vm.removeLeadingZeros('000')).toBe('0');
Expand Down Expand Up @@ -37,11 +37,33 @@ describe('CurrencyInput.vue', () => {

describe('NumberInput getCypressValue()', () => {
it('contains cypress Value', () => {
const wrapper = mount(Component, {
const wrapper = mount(CurrencyInput, {
props: {
cypressId: 'potato'
}
});
expect(wrapper.find("[data-cy=potato]").exists()).toBe(true)
});
});

describe("CurrencyInput event handling", () => {
it("works correctly with v-model", async () => {
const wrapper = mount({
data() {
return { currency: "11" };
},
template: '<div><CurrencyInput v-model="currency" /></div>',
components: { CurrencyInput },
});

expect(wrapper.vm.currency).toBe("11");
const baseInput = wrapper.find("input");
await baseInput.setValue("1111");

// Model value is unformatted
expect(wrapper.vm.currency).toBe("1111");

// Displayed value is formatted
expect(baseInput.element.value).toBe("1,111")
});
});