71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import { TICKS_PER_REVOLUTION, useMotor } from 'src/composables/motor';
|
|
import { describe, expect, test } from 'vitest';
|
|
import { nextTick, ref } from 'vue';
|
|
import { toggleTickAsync, usePulseCount } from '../utils';
|
|
|
|
describe('motor', () => {
|
|
test('starts in the off state', () => {
|
|
const isOn = ref(false);
|
|
const tick = ref(false);
|
|
|
|
const { pulse } = useMotor(tick, isOn);
|
|
const { pulseCount } = usePulseCount(pulse);
|
|
|
|
expect(pulseCount.value).toBe(0);
|
|
});
|
|
|
|
test('does not pulse when off', async () => {
|
|
const isOn = ref(false);
|
|
const tick = ref(false);
|
|
|
|
const { pulse } = useMotor(tick, isOn);
|
|
const { pulseCount } = usePulseCount(pulse);
|
|
|
|
for (let i = 0; i < 3 * TICKS_PER_REVOLUTION; i++) {
|
|
await toggleTickAsync(tick);
|
|
}
|
|
expect(pulseCount.value).toBe(0);
|
|
});
|
|
|
|
test('does pulse when on', async () => {
|
|
const isOn = ref(true);
|
|
const tick = ref(false);
|
|
|
|
const { pulse } = useMotor(tick, isOn);
|
|
const { pulseCount } = usePulseCount(pulse);
|
|
|
|
for (let i = 0; i < 3 * TICKS_PER_REVOLUTION; i++) {
|
|
await toggleTickAsync(tick);
|
|
|
|
const expectedValue = Math.floor((i + 1) / TICKS_PER_REVOLUTION);
|
|
expect(pulseCount.value).toBe(expectedValue);
|
|
}
|
|
});
|
|
|
|
test('stops pulsing when turned off', async () => {
|
|
const tick = ref(false);
|
|
const isOn = ref(true);
|
|
|
|
const { pulse } = useMotor(tick, isOn);
|
|
const { pulseCount } = usePulseCount(pulse);
|
|
|
|
for (let i = 0; i < 3 * TICKS_PER_REVOLUTION; i++) {
|
|
await toggleTickAsync(tick);
|
|
|
|
const expectedValue = Math.floor((i + 1) / TICKS_PER_REVOLUTION);
|
|
expect(pulseCount.value).toBe(expectedValue);
|
|
}
|
|
|
|
const pulseValueAtStop = pulseCount.value;
|
|
|
|
isOn.value = false;
|
|
await nextTick();
|
|
|
|
for (let i = 0; i < 3 * TICKS_PER_REVOLUTION; i++) {
|
|
toggleTickAsync(tick);
|
|
}
|
|
|
|
expect(pulseCount.value).toBe(pulseValueAtStop);
|
|
});
|
|
});
|