running-tools

A collection of tools for runners and their coaches
git clone https://git.ashermorgan.net/running-tools/
Log | Files | Refs | README

commit eccf725222839ab2f209cafd7b2c8768563abf52
parent 319213b0729bec524d4edbfb14b6557c138148e7
Author: ashermorgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Sat, 24 Feb 2024 13:21:46 -0800

Improve component unit tests

Diffstat:
Mtests/unit/components/SimpleTargetTable.spec.js | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Mtests/unit/components/TargetEditor.spec.js | 67++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mtests/unit/components/TargetSetSelector.spec.js | 2--
Mtests/unit/components/TimeInput.spec.js | 2--
4 files changed, 113 insertions(+), 25 deletions(-)

diff --git a/tests/unit/components/SimpleTargetTable.spec.js b/tests/unit/components/SimpleTargetTable.spec.js @@ -1,7 +1,5 @@ -/* eslint-disable no-underscore-dangle */ - import { test, expect } from 'vitest'; -import { shallowMount } from '@vue/test-utils'; +import { mount, shallowMount } from '@vue/test-utils'; import SimpleTargetTable from '@/components/SimpleTargetTable.vue'; test('results should be correct and sorted by time', () => { @@ -9,24 +7,65 @@ test('results should be correct and sorted by time', () => { const wrapper = shallowMount(SimpleTargetTable, { propsData: { calculateResult: (row) => ({ - distanceValue: row.distanceValue, - distanceUnit: row.distanceUnit, - time: row.distanceValue + 1, + distanceValue: row.distanceValue ? row.distanceValue : row.time + 1, + distanceUnit: row.distanceUnit ? row.distanceUnit : 'miles', + time: row.time ? row.time : row.distanceValue + 1, + result: row.result, }), targets: [ - { distanceValue: 20, distanceUnit: 'meters' }, - { distanceValue: 100, distanceUnit: 'meters' }, - { distanceValue: 1, distanceUnit: 'kilometers' }, - { distanceValue: 10, distanceUnit: 'meters' }, + { result: 'time', distanceValue: 20, distanceUnit: 'meters' }, + { result: 'time', distanceValue: 100, distanceUnit: 'meters' }, + { result: 'time', distanceValue: 1, distanceUnit: 'kilometers' }, + { result: 'time', distanceValue: 10, distanceUnit: 'meters' }, + { result: 'distance', time: 1 }, + { result: 'distance', time: 10 }, ], }, }); // Assert results are correct expect(wrapper.vm.results).to.deep.equal([ - { distanceValue: 1, distanceUnit: 'kilometers', time: 2 }, - { distanceValue: 10, distanceUnit: 'meters', time: 11 }, - { distanceValue: 20, distanceUnit: 'meters', time: 21 }, - { distanceValue: 100, distanceUnit: 'meters', time: 101 }, + { result: 'distance', distanceValue: 2, distanceUnit: 'miles', time: 1 }, + { result: 'time', distanceValue: 1, distanceUnit: 'kilometers', time: 2 }, + { result: 'distance', distanceValue: 11, distanceUnit: 'miles', time: 10 }, + { result: 'time', distanceValue: 10, distanceUnit: 'meters', time: 11 }, + { result: 'time', distanceValue: 20, distanceUnit: 'meters', time: 21 }, + { result: 'time', distanceValue: 100, distanceUnit: 'meters', time: 101 }, ]); }); + +test('getPace should return correct imerial paces', () => { + // Initialize component + const wrapper = mount(SimpleTargetTable, { + propsData: { + calculateResult: () => {}, + defaultUnitSystem: 'imperial', + }, + }); + + // Assert paces are correct + const result = wrapper.vm.getPace({ + distanceValue: 1, + distanceUnit: 'kilometers', + time: 300, + }); + expect(result).to.be.closeTo(482.81, 0.01); +}); + +test('getPace should return correct metric paces', () => { + // Initialize component + const wrapper = mount(SimpleTargetTable, { + propsData: { + calculateResult: () => {}, + defaultUnitSystem: 'metric', + }, + }); + + // Assert paces are correct + const result = wrapper.vm.getPace({ + distanceValue: 1, + distanceUnit: 'miles', + time: 600, + }); + expect(result).to.be.closeTo(372.82, 0.01); +}); diff --git a/tests/unit/components/TargetEditor.spec.js b/tests/unit/components/TargetEditor.spec.js @@ -1,10 +1,30 @@ -/* eslint-disable no-underscore-dangle */ - import { test, expect } from 'vitest'; import { shallowMount, mount } from '@vue/test-utils'; import TargetEditor from '@/components/TargetEditor.vue'; -test('addDistanceTarget method should correctly add distance target', async () => { +test('revert method should emit revert event', async () => { + // Initialize component + const wrapper = shallowMount(TargetEditor); + + // Call revert method + await wrapper.vm.revert(); + + // Assert revert event was emitted + expect(wrapper.emitted().revert.length).to.equal(1); +}); + +test('close method should emit close event', async () => { + // Initialize component + const wrapper = shallowMount(TargetEditor); + + // Call close method + await wrapper.vm.close(); + + // Assert close event was emitted + expect(wrapper.emitted().close.length).to.equal(1); +}); + +test('addDistanceTarget method should correctly add imperial distance target', async () => { // Initialize component const wrapper = mount(TargetEditor, { propsData: { @@ -35,6 +55,37 @@ test('addDistanceTarget method should correctly add distance target', async () = ]); }); +test('addDistanceTarget method should correctly add metric distance target', async () => { + // Initialize component + const wrapper = mount(TargetEditor, { + propsData: { + modelValue: { + name: 'My target set', + targets: [ + { distanceUnit: 'miles', distanceValue: 0, result: 'time' }, + { time: 0, result: 'distance' }, + ], + }, + defaultUnitSystem: 'metric' + }, + }); + + // Add distance target + await wrapper.vm.addDistanceTarget(); + + // Assert input event was emitted + expect(wrapper.emitted()['update:modelValue']).to.deep.equal([ + [{ + name: 'My target set', + targets: [ + { distanceUnit: 'miles', distanceValue: 0, result: 'time' }, + { time: 0, result: 'distance' }, + { distanceUnit: 'kilometers', distanceValue: 1, result: 'time'}, + ], + }], + ]); +}); + test('addTimeTarget method should correctly add time target', async () => { // Initialize component const wrapper = mount(TargetEditor, { @@ -64,7 +115,7 @@ test('addTimeTarget method should correctly add time target', async () => { ]); }); -test('should emit input event when targets are updated', async () => { +test('Should emit input event when targets are updated', async () => { // Initialize component const wrapper = mount(TargetEditor, { propsData: { @@ -78,7 +129,8 @@ test('should emit input event when targets are updated', async () => { }); // Update distance value - await wrapper.find('tbody input').trigger('keydown', { key: 'ArrowUp' }); + wrapper.vm.internalValue.targets[0].distanceValue = 3; + await wrapper.vm.$nextTick(); // Assert input event was emitted expect(wrapper.emitted()['update:modelValue']).to.deep.equal([ @@ -93,7 +145,7 @@ test('should emit input event when targets are updated', async () => { ]); }); -test('should emit input event when target set name is updated', async () => { +test('Should emit input event when target set name is updated', async () => { // Initialize component const wrapper = mount(TargetEditor, { propsData: { @@ -107,7 +159,8 @@ test('should emit input event when target set name is updated', async () => { }); // Update distance value - await wrapper.find('thead input').setValue('My target set #2'); + wrapper.vm.internalValue.name = 'My target set #2'; + await wrapper.vm.$nextTick(); // Assert input event was emitted expect(wrapper.emitted()['update:modelValue']).to.deep.equal([ diff --git a/tests/unit/components/TargetSetSelector.spec.js b/tests/unit/components/TargetSetSelector.spec.js @@ -1,5 +1,3 @@ -/* eslint-disable no-underscore-dangle */ - import { test, expect } from 'vitest'; import { mount } from '@vue/test-utils'; import TargetSetSelector from '@/components/TargetSetSelector.vue'; diff --git a/tests/unit/components/TimeInput.spec.js b/tests/unit/components/TimeInput.spec.js @@ -1,5 +1,3 @@ -/* eslint-disable no-underscore-dangle */ - import { test, expect } from 'vitest'; import { shallowMount, mount } from '@vue/test-utils'; import TimeInput from '@/components/TimeInput.vue';