timered-counter
Version:
Make the value change more vivid and natural
82 lines • 3.73 kB
JavaScript
import { addMilliseconds } from 'date-fns';
import { expect } from '@storybook/test';
import { sleep } from '../utils/index.js';
import { DurationPartMillisecond, DurationPartType, } from '../../src/types/duration.js';
export async function datetimePrecision(context, { counter, setBy, }) {
const { step, args } = context;
const precisionTable = [
[
DurationPartType.Second,
[],
[],
[],
[],
[],
[],
],
[
DurationPartType.Minute,
[],
[],
[],
[],
[],
],
[
DurationPartType.Hour,
[],
[],
[],
[],
],
[
DurationPartType.Day,
[],
[],
[],
],
[
DurationPartType.Month,
[],
[],
],
[]],
];
const DurationPartTypeLevelMap = {
[]: 0,
[]: 1,
[]: 2,
[]: 3,
[]: 4,
[]: 5,
[]: 6,
[]: 7,
[]: 8,
};
await step('Test different precision', async () => {
for await (const [singlePrecision, ...pairPrecisions] of precisionTable) {
await step(`Single parameter: ${singlePrecision}`, async () => {
setBy(counter, 'precision', singlePrecision);
setBy(counter, 'value', [
new Date(),
addMilliseconds(new Date(), DurationPartMillisecond[singlePrecision]),
]);
await sleep((args.animationOptions.duration ?? 100) + 10);
await expect(counter.parts.length).toBe(1);
});
await step(`Pair precision: ${singlePrecision}`, async () => {
for await (const pairPrecision of pairPrecisions) {
const [min, max] = pairPrecision;
setBy(counter, 'precision', JSON.stringify([min, max]));
setBy(counter, 'value', [
new Date(),
addMilliseconds(new Date(), DurationPartMillisecond[max] + 1),
]);
await sleep((args.animationOptions.duration ?? 100) + 10);
await expect(counter.parts.length).toBe(DurationPartTypeLevelMap[max] - DurationPartTypeLevelMap[min] + 1);
}
});
}
});
}
//# sourceMappingURL=datetime-precision.js.map