|
1 | | -import Vue from 'vue' |
2 | | -import FloatLabel from 'components/FloatLabel' |
| 1 | +import { ctorInput, ctorTextArea, ctorSelect } from '../helpers' |
3 | 2 |
|
4 | | -const getFloatLabel = () => { |
5 | | - return new Vue({ |
6 | | - components: { FloatLabel }, |
7 | | - render: h => { |
8 | | - return h('float-label', [ |
9 | | - h('input', { |
10 | | - attrs: { |
11 | | - type: 'text', |
12 | | - placeholder: 'Name' |
13 | | - } |
14 | | - }) |
15 | | - ]) |
16 | | - } |
17 | | - }).$mount().$children[0] |
18 | | -} |
| 3 | +test('label', () => { |
| 4 | + expect(ctorInput().label).toEqual('Name') |
| 5 | + expect(ctorTextArea().label).toEqual('Comment') |
| 6 | + expect(ctorSelect().label).toEqual('Framework') |
| 7 | +}) |
| 8 | + |
| 9 | +test('formEl', () => { |
| 10 | + expect(ctorInput().formEl).toBeInstanceOf(HTMLInputElement) |
| 11 | + expect(ctorTextArea().formEl).toBeInstanceOf(HTMLTextAreaElement) |
| 12 | + expect(ctorSelect().formEl).toBeInstanceOf(HTMLSelectElement) |
| 13 | +}) |
19 | 14 |
|
20 | | -test('setup on mount', () => { |
21 | | - const vm = getFloatLabel() |
22 | | - expect(vm.input).toBeInstanceOf(HTMLInputElement) |
23 | | - expect(vm.label).toEqual('Name') |
| 15 | +test('formElType', () => { |
| 16 | + expect(ctorInput().formElType).toEqual('input') |
| 17 | + expect(ctorTextArea().formElType).toEqual('textarea') |
| 18 | + expect(ctorSelect().formElType).toEqual('select') |
24 | 19 | }) |
25 | 20 |
|
26 | 21 | test('classObject', () => { |
27 | | - const vm = getFloatLabel() |
| 22 | + const vm = ctorInput() |
28 | 23 | const classObjectKeys = Object.keys(vm.classObject) |
29 | 24 | expect(classObjectKeys).toContain('vfl-label-on-input') |
30 | 25 | expect(classObjectKeys).toContain('vfl-label-on-focus') |
31 | 26 | }) |
32 | 27 |
|
33 | | -test('focusInput', () => { |
34 | | - const vm = getFloatLabel() |
35 | | - vm.input.focus = jest.fn() |
36 | | - vm.focusInput() |
37 | | - expect(vm.input.focus).toHaveBeenCalledTimes(1) |
| 28 | +test('focusFormEl', () => { |
| 29 | + const vm = ctorInput() |
| 30 | + vm.formEl.focus = jest.fn() |
| 31 | + vm.focusFormEl() |
| 32 | + expect(vm.formEl.focus).toHaveBeenCalledTimes(1) |
38 | 33 | }) |
39 | 34 |
|
40 | 35 | describe('updateHasValue', () => { |
41 | 36 | test('hasValue is true when element value isnt empty', () => { |
42 | | - const vm = getFloatLabel() |
43 | | - const event = { target: vm.input } |
44 | | - vm.input.value = 'Foo' |
| 37 | + const vm = ctorInput() |
| 38 | + const event = { target: vm.formEl } |
| 39 | + vm.formEl.value = 'Foo' |
45 | 40 | vm.updateHasValue(event) |
46 | 41 | expect(vm.hasValue).toEqual(true) |
47 | 42 | }) |
48 | 43 |
|
49 | 44 | test('hasValue is false when element value is empty', () => { |
50 | | - const vm = getFloatLabel() |
51 | | - const event = { target: vm.input } |
52 | | - vm.input.value = '' |
| 45 | + const vm = ctorInput() |
| 46 | + const event = { target: vm.formEl } |
| 47 | + vm.formEl.value = '' |
53 | 48 | vm.updateHasValue(event) |
54 | 49 | expect(vm.hasValue).toEqual(false) |
55 | 50 | }) |
56 | 51 | }) |
57 | 52 |
|
58 | 53 | describe('updateIsActive', () => { |
59 | 54 | test('isActive is true when element is focused and has content', () => { |
60 | | - const vm = getFloatLabel() |
61 | | - const event = { target: vm.input } |
| 55 | + const vm = ctorInput() |
| 56 | + const event = { target: vm.formEl } |
62 | 57 | vm.hasValue = true |
63 | | - vm.input.focus() |
| 58 | + vm.formEl.focus() |
64 | 59 | vm.updateIsActive(event) |
65 | 60 |
|
66 | 61 | expect(vm.isActive).toEqual(true) |
67 | 62 | }) |
68 | 63 |
|
69 | 64 | test('isActive is false when element isnt focused and has content', () => { |
70 | | - const vm = getFloatLabel() |
71 | | - const event = { target: vm.input } |
| 65 | + const vm = ctorInput() |
| 66 | + const event = { target: vm.formEl } |
72 | 67 | vm.hasValue = true |
73 | 68 | vm.updateIsActive(event) |
74 | 69 |
|
75 | 70 | expect(vm.isActive).toEqual(false) |
76 | 71 | }) |
77 | 72 |
|
78 | 73 | test('isActive is false when element isnt focused and doesnt have content', () => { |
79 | | - const vm = getFloatLabel() |
80 | | - const event = { target: vm.input } |
| 74 | + const vm = ctorInput() |
| 75 | + const event = { target: vm.formEl } |
81 | 76 | vm.updateIsActive(event) |
82 | 77 |
|
83 | 78 | expect(vm.isActive).toEqual(false) |
|
0 commit comments