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

fix bug in bhText formatters #179

Merged
merged 3 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 13 additions & 5 deletions src/bh/components/BhText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
data() {
return {
className: 'bh-text',
val: this.formatters.reduce((c, f) => f(c), this.value || ''),
val: this.value,
};
},
computed: {
Expand All @@ -56,21 +56,29 @@ export default {

return classes;
},
formattedValue() {
return this.formatters.reduce((c, f) => f(c), this.value || '');
},
},
watch: {
value(value) {
this.val = value;
value() {
this.val = this.formattedValue;
},
val(value) {
this.$emit('input', value);
},
formatters() {
this.$nextTick();
this.val = this.formattedValue;
},
},
mounted() {
const { input } = this.$refs;
this.val = this.formattedValue;

const { input } = this.$refs;
if (input) {
input.oninput = async () => {
const formattedValue = this.formatters.reduce((c, f) => f(c), input.value);
const formattedValue = this.formatters.reduce((c, f) => f(c), input.value || '');
const { selectionStart } = input;
if (input.value !== formattedValue) {
this.val = formattedValue;
Expand Down
92 changes: 86 additions & 6 deletions test/unit/specs/bh/BhText.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,95 @@ import BhText from 'bh/components/BhText';
const localVue = createLocalVue();
localVue.use(BH);

const formmater1 = v => v.replace(' ', '_');

describe('BhText.vue', () => {
let wrapper;
beforeEach(() => {
wrapper = shallowMount(BhText, {
localVue,
describe('without initial formatters', () => {
let wrapper;
beforeEach(() => {
wrapper = shallowMount(BhText, {
localVue,
});
});

test('renders correctly', () => {
expect(wrapper).toMatchSnapshot();
});

describe('with formatters', () => {
beforeEach(() => {
wrapper.setProps({
formatters: [
formmater1,
],
});
});

describe('value is "ok ok"', () => {
beforeEach(() => {
wrapper.setProps({
value: 'ok ok',
});
});

test('value is formatted to "ok_ok"', () => {
const emitted = wrapper.emitted('input');
expect(emitted).toBeDefined();
expect(emitted[0][0]).toBe('ok_ok');
});
});
});

describe('value is "ok ok"', () => {
beforeEach(() => {
wrapper.setProps({
value: 'ok ok',
});
});

describe('add formatters', () => {
beforeEach(() => {
wrapper.setProps({
formatters: [
formmater1,
],
});
});

test('value is formatted to "ok_ok"', () => {
const emitted = wrapper.emitted('input');
expect(emitted).toBeDefined();
expect(emitted[emitted.length - 1][0]).toBe('ok_ok');
});
});
});
});

test('renders correctly', () => {
expect(wrapper).toMatchSnapshot();
describe('with initial formatters', () => {
let wrapper;
beforeEach(() => {
wrapper = shallowMount(BhText, {
localVue,
propsData: {
formatters: [
formmater1,
],
},
});

describe('value is "ok ok"', () => {
beforeEach(() => {
wrapper.setProps({
value: 'ok ok',
});
});

test('value is formatted to "ok_ok"', () => {
const emitted = wrapper.emitted('input');
expect(emitted).toBeDefined();
expect(emitted[0][0]).toBe('ok_ok');
});
});
});
});
});
2 changes: 1 addition & 1 deletion test/unit/specs/bh/__snapshots__/BhText.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BhText.vue renders correctly 1`] = `
exports[`BhText.vue without initial formatters renders correctly 1`] = `
<bhinput-stub>
<div class="bh-text">
<!---->
Expand Down