Merge branch 'main' of https://github.com/appwrite/appwrite-console-poc into feat-track-error-events

This commit is contained in:
Torsten Dittmann committed 2023-02-13 13:48:38 +01:00
commit 6b94d8ea44
9 files changed
+600 -236

No files matched your search

+1 -1
View File
@@ -18,7 +18,7 @@
let element: HTMLInputElement;
let error: string;
const pattern = String.raw`^[^]+\s+[^]+\s+[^]+\s+[^]+\s+[^]`;
const pattern = String.raw`^[^]+\s+[^]+\s+[^]+\s+[^]+\s+[^]+`;
onMount(() => {
if (element && autofocus) {
+33 -3
View File
@@ -1,3 +1,10 @@
/**
* Formats the time in a human readable format.
*
* @export
* @param {number} time - The time in seconds.
* @returns {string} The formatted time string.
*/
export function calculateTime(time: number) {
const milliseconds = Math.floor(time * 1000);
const seconds = Math.floor(time);
@@ -20,11 +27,13 @@ export function calculateTime(time: number) {
} else if (months < 12) {
return `${months}M`;
} else {
return `${years} y`;
return `${years}y`;
}
}
export function timeToSeconds(time: number, unit: string) {
type Unit = 'ms' | 's' | 'm' | 'h' | 'd' | 'M' | 'y';
export function timeToSeconds(time: number, unit: Unit) {
switch (unit) {
case 'ms':
return time / 1000;
@@ -44,7 +53,7 @@ export function timeToSeconds(time: number, unit: string) {
return time;
}
}
export function timeToMinutes(time: number, unit: string) {
export function timeToMinutes(time: number, unit: Unit) {
switch (unit) {
case 'ms':
return time / 1000 / 60;
@@ -64,3 +73,24 @@ export function timeToMinutes(time: number, unit: string) {
return time;
}
}
export function secsToUnit(time: number, unit: Unit) {
switch (unit) {
case 'ms':
return time * 1000;
case 's':
return time;
case 'm':
return time / 60;
case 'h':
return time / 60 / 60;
case 'd':
return time / 60 / 60 / 24;
case 'M':
return time / 60 / 60 / 24 / 30;
case 'y':
return time / 60 / 60 / 24 / 30 / 12;
default:
return time;
}
}
@@ -1,237 +1,12 @@
<script lang="ts">
import { CardGrid, Heading } from '$lib/components';
import { Pill } from '$lib/elements/';
import { Button, InputNumber, InputSelect, Form } from '$lib/elements/forms';
import { Container } from '$lib/layout';
import { project } from '../../store';
import { addNotification } from '$lib/stores/notifications';
import { sdkForConsole } from '$lib/stores/sdk';
import { clickOnEnter } from '$lib/helpers/a11y';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { timeToSeconds } from '$lib/helpers/timeConversion';
import { writable } from 'svelte/store';
const projectId = $project.$id;
let isLimited = $project.authLimit === 0 ? 'unlimited' : 'limited';
let newLimit = $project.authLimit === 0 ? 100 : $project.authLimit;
let time = $project.authDuration;
let timeInSec = time;
let period = writable('s');
let btnActive = false;
let maxSessions = $project.authSessionsLimit;
let options = [
{ label: 'days', value: 'd' },
{ label: 'hours', value: 'h' },
{ label: 'minutes', value: 'm' },
{ label: 'seconds', value: 's' }
];
$: {
if (isLimited) {
if (isLimited === 'unlimited' && $project.authLimit === 0) {
btnActive = false;
} else if (isLimited === 'limited' && $project.authLimit === newLimit) {
btnActive = false;
} else {
btnActive = true;
}
}
}
async function updateLimit() {
try {
await sdkForConsole.projects.updateAuthLimit(
projectId,
isLimited === 'unlimited' ? 0 : newLimit
);
invalidate(Dependencies.PROJECT);
btnActive = false;
addNotification({
type: 'success',
message: 'Updated project users limit successfully'
});
trackEvent(Submit.AuthLimitUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.AuthLimitUpdate);
}
}
async function updateSessionLength() {
try {
await sdkForConsole.projects.updateAuthDuration(
projectId,
timeToSeconds(time, $period)
);
invalidate(Dependencies.PROJECT);
$period = 's';
addNotification({
type: 'success',
message: 'Updated project users limit successfully'
});
trackEvent(Submit.SessionsLengthUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.SessionsLengthUpdate);
}
}
async function updateSessionsLimit() {
try {
await sdkForConsole.projects.updateAuthSessionsLimit(projectId, maxSessions);
addNotification({
type: 'success',
message: 'Sessions limit has been updated'
});
trackEvent(Submit.SessionsLimitUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.SessionsLimitUpdate);
}
}
period.subscribe((p) => {
if (p === 'd') {
time = timeInSec / 86400;
timeInSec = timeToSeconds(time, p);
} else if (p === 'h') {
time = timeInSec / 3600;
timeInSec = timeToSeconds(time, p);
} else if (p === 'm') {
time = timeInSec / 60;
timeInSec = timeToSeconds(time, p);
} else if (p === 's') {
time = timeInSec;
timeInSec = timeToSeconds(time, p);
}
});
$: if (time) {
timeInSec = timeToSeconds(time, $period);
}
import UpdateSessionLength from './updateSessionLength.svelte';
import UpdateSessionsLimit from './updateSessionsLimit.svelte';
import UpdateUsersLimit from './updateUsersLimit.svelte';
</script>
<Container>
<CardGrid>
<Heading tag="h2" size="6">Users Limit</Heading>
<p>
Limit new users from signing up for your project, regardless of authentication method.
You can still create users and team memberships from your Appwrite console.
<b> The maximum limit is 10,000 users.</b>
</p>
<svelte:fragment slot="aside">
<ul class="form-list">
<li class="form-item">
<label class="choice-item" for="unlimited">
<input
id="unlimited"
name="authLimit"
class=" u-cross-child-center"
required
type="radio"
checked={isLimited === 'unlimited'}
bind:group={isLimited}
value="unlimited" />
<div class="choice-item-content u-cross-child-center">
<div class="choice-item-title">Unlimited</div>
</div>
<Pill>recommended</Pill>
</label>
</li>
<li class="form-item is-multiple">
<div class="input-text-wrapper">
<label class="choice-item" for="limited">
<input
id="limited"
name="authLimit"
required
type="radio"
checked={isLimited === 'limited'}
bind:group={isLimited}
value="limited" />
<div class="choice-item-content">
<div class="choice-item-title">Limited</div>
</div>
</label>
</div>
<div
class="input-text-wrapper u-stretch"
on:click={() => (isLimited = 'limited')}
on:keyup|self={clickOnEnter}>
<input
type="number"
name="limit"
id="limit"
class="input-text"
max="10000"
disabled={isLimited === 'unlimited'}
bind:value={newLimit} />
</div>
</li>
</ul>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button
disabled={!btnActive}
on:click={() => {
updateLimit();
}}>Update</Button>
</svelte:fragment>
</CardGrid>
<CardGrid>
<Heading tag="h2" size="6">Session Length</Heading>
<p>
If you reduce the limit, users who are currently logged in will be logged out of the
application.
</p>
<svelte:fragment slot="aside">
<form class="form u-grid u-gap-16">
<ul class="form-list is-multiple">
<InputNumber id="length" label="Length" bind:value={time} />
<InputSelect id="period" label="Time Period" bind:value={$period} {options} />
</ul>
</form>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button
disabled={$period === 's' && time === $project.authDuration}
on:click={() => {
updateSessionLength();
}}>
Update
</Button>
</svelte:fragment>
</CardGrid>
<Form on:submit={updateSessionsLimit}>
<CardGrid>
<Heading tag="h2" size="6">Session Limit</Heading>
<p>Maximum number of active sessions allowed per user.</p>
<svelte:fragment slot="aside">
<ul>
<InputNumber id="max-session" label="Limit" bind:value={maxSessions} />
</ul>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button disabled={maxSessions === $project.authSessionsLimit} submit>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>
<UpdateUsersLimit />
<UpdateSessionLength />
<UpdateSessionsLimit />
</Container>
@@ -0,0 +1,105 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { CardGrid, Heading } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Button, InputNumber, InputSelect } from '$lib/elements/forms';
import { secsToUnit, timeToSeconds } from '$lib/helpers/timeConversion';
import { addNotification } from '$lib/stores/notifications';
import { sdkForConsole } from '$lib/stores/sdk';
import { writable } from 'svelte/store';
import { project } from '../../store';
enum Period {
Days = 'd',
Hours = 'h',
Minutes = 'm',
Seconds = 's'
}
const projectId = $project.$id;
const options = [
{ label: 'days', value: Period.Days },
{ label: 'hours', value: Period.Hours },
{ label: 'minutes', value: Period.Minutes },
{ label: 'seconds', value: Period.Seconds }
];
let period = writable(Period.Seconds);
function getInitialTime() {
let t = $project.authDuration;
if (t >= 86400 && t % 86400 === 0) {
$period = Period.Days;
return t / 86400;
}
if (t >= 3600 && t % 3600 === 0) {
$period = Period.Hours;
return t / 3600;
}
if (t >= 60 && t % 60 === 0) {
$period = Period.Minutes;
return t / 60;
}
return t;
}
let time = getInitialTime();
let timeInSecs = timeToSeconds(time, $period);
period.subscribe((p) => {
time = secsToUnit(timeInSecs, p);
});
$: timeInSecs = timeToSeconds(time, $period);
async function updateSessionLength() {
try {
await sdkForConsole.projects.updateAuthDuration(projectId, timeInSecs);
invalidate(Dependencies.PROJECT);
addNotification({
type: 'success',
message: 'Updated project users limit successfully'
});
trackEvent(Submit.SessionsLengthUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.SessionsLengthUpdate);
}
}
</script>
<CardGrid>
<Heading tag="h2" size="6">Session Length</Heading>
<p>
If you reduce the limit, users who are currently logged in will be logged out of the
application.
</p>
<svelte:fragment slot="aside">
<form class="form u-grid u-gap-16">
<ul class="form-list is-multiple">
<InputNumber id="length" label="Length" bind:value={time} />
<InputSelect id="period" label="Time Period" bind:value={$period} {options} />
</ul>
</form>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button
disabled={timeToSeconds(time, $period) === $project.authDuration}
on:click={() => {
updateSessionLength();
}}>
Update
</Button>
</svelte:fragment>
</CardGrid>
@@ -0,0 +1,46 @@
<script lang="ts">
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { CardGrid, Heading } from '$lib/components';
import { Button, Form, InputNumber } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdkForConsole } from '$lib/stores/sdk';
import { project } from '../../store';
const projectId = $project.$id;
let maxSessions = $project.authSessionsLimit;
async function updateSessionsLimit() {
try {
await sdkForConsole.projects.updateAuthSessionsLimit(projectId, maxSessions);
addNotification({
type: 'success',
message: 'Sessions limit has been updated'
});
trackEvent(Submit.SessionsLimitUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.SessionsLimitUpdate);
}
}
</script>
<Form on:submit={updateSessionsLimit}>
<CardGrid>
<Heading tag="h2" size="6">Sessions Limit</Heading>
<p>Maximum number of active sessions allowed per user.</p>
<svelte:fragment slot="aside">
<ul>
<InputNumber id="max-session" label="Limit" bind:value={maxSessions} />
</ul>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button disabled={maxSessions === $project.authSessionsLimit} submit>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>
@@ -0,0 +1,117 @@
<script lang="ts">
import { invalidate } from '$app/navigation';
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { CardGrid, Heading } from '$lib/components';
import { Dependencies } from '$lib/constants';
import { Pill } from '$lib/elements';
import { Button } from '$lib/elements/forms';
import { clickOnEnter } from '$lib/helpers/a11y';
import { addNotification } from '$lib/stores/notifications';
import { sdkForConsole } from '$lib/stores/sdk';
import { project } from '../../store';
const projectId = $project.$id;
let isLimited = $project.authLimit !== 0;
let newLimit = isLimited ? $project.authLimit : 100;
let submitting = false;
$: btnDisabled = (function isBtnDisabled() {
if (
submitting ||
(!isLimited && $project.authLimit === 0) ||
(isLimited && $project.authLimit === newLimit)
) {
return true;
}
return false;
})();
async function updateLimit() {
submitting = true;
try {
await sdkForConsole.projects.updateAuthLimit(projectId, isLimited ? newLimit : 0);
invalidate(Dependencies.PROJECT).then(() => (submitting = false));
addNotification({
type: 'success',
message: 'Updated project users limit successfully'
});
trackEvent(Submit.AuthLimitUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
submitting = false;
trackError(error, Submit.AuthLimitUpdate);
}
}
</script>
<CardGrid>
<Heading tag="h2" size="6">Users Limit</Heading>
<p>
Limit new users from signing up for your project, regardless of authentication method. You
can still create users and team memberships from your Appwrite console.
<b>The maximum limit is 10,000 users.</b>
</p>
<svelte:fragment slot="aside">
<ul class="form-list">
<li class="form-item">
<label class="choice-item" for="unlimited">
<input
id="unlimited"
name="authLimit"
class="u-cross-child-center"
required
type="radio"
bind:group={isLimited}
value={false} />
<div class="choice-item-content u-cross-child-center">
<div class="choice-item-title">Unlimited</div>
</div>
<Pill>recommended</Pill>
</label>
</li>
<li class="form-item is-multiple">
<div class="input-text-wrapper">
<label class="choice-item" for="limited">
<input
id="limited"
name="authLimit"
required
type="radio"
bind:group={isLimited}
value={true} />
<div class="choice-item-content">
<div class="choice-item-title">Limited</div>
</div>
</label>
</div>
<div
class="input-text-wrapper u-stretch"
on:click={() => (isLimited = true)}
on:keyup|self={clickOnEnter}>
<input
type="number"
name="limit"
id="limit"
class="input-text"
max="10000"
disabled={!isLimited}
bind:value={newLimit} />
</div>
</li>
</ul>
</svelte:fragment>
<svelte:fragment slot="actions">
<Button
disabled={btnDisabled}
on:click={() => {
updateLimit();
}}>Update</Button>
</svelte:fragment>
</CardGrid>
@@ -113,7 +113,7 @@
<WizardStep {beforeSubmit}>
<svelte:fragment slot="title">Register your Flutter app</svelte:fragment>
<svelte:fragment slot="subtitle">
<div class="u-flex u-gap-16 u-margin-block-start-8">
<div class="u-flex u-gap-16 u-margin-block-start-8 u-flex-wrap">
<Pill
button
on:click={() => (platform = Platform.Android)}
+121
View File
@@ -0,0 +1,121 @@
import '@testing-library/jest-dom';
import { render } from '@testing-library/svelte';
import userEvent from '@testing-library/user-event';
import { InputCron } from '../../../src/lib/elements/forms';
const validStrings = [
'*/2 */2 * * *',
'* * * * *',
'* 20,21,22 * * *',
// Handles CSV values
'* 20,22 * * *',
// CSV values can be complex
'7-9 * */9 * *',
// 15th minute, of the second hour, every 15 days, in January, every Friday
'1 * * * 7',
// Test with exact times
'47 21 * * *',
// Test Day of the week
// According cron implementation, 0|7 = sunday, 1 => monday, etc
'* * * * 0',
'* * * * 7',
'* * * * 1',
// Should return the sunday date as 7 equals 0
'0 0 * * MON,SUN',
'0 0 * * 1,7',
'0 0 * * 0-4',
'0 0 * * 7-4',
'0 0 * * 4-7',
'0 0 * * 7-3',
'0 0 * * 3-7',
'0 0 * * 3-7',
// Test lists of values and ranges (Abhoryo)
'0 0 * * 2-7',
'0 0 * * 2-7',
'0 0 * * 4-7',
// Test increments of ranges
'0-12/4 * * * *',
'4-59/2 * * * *',
'4-59/2 * * * *',
'4-59/3 * * * *',
// Test Day of the Week and the Day of the Month
'0 0 1 1 0',
'0 0 1 JAN 0',
'0 0 1 * 0',
// Test the W day of the week modifier for day of the month field
'0 0 2W * *',
'0 0 1W * *',
'0 0 1W * *',
'0 0 3W * *',
'0 0 16W * *',
'0 0 28W * *',
'0 0 30W * *',
'0 0 31W * *',
// Test the last weekday of a month
'* * * * 5L',
'* * * * 6L',
'* * * * 7L',
'* * * * 1L',
'* * * 1 5L',
// Test the hash symbol for the nth weekday of a given month
'* * * * 5#2',
'* * * * 5#1',
'* * * * 3#4',
// make sure that casing is less relevant for shortcuts, months, and days
'0 1 15 JUL mon,Wed,FRi',
'0 1 15 jul mon,Wed,FRi',
//DOW and DOM do not support ?
'0 12 * * ?',
'0 12 ? * *',
'0-59/59 10 * * *',
'0-59/59 10 * * *',
'0-59/59 10 * * *',
'0-59/65 10 * * *',
'41-59/24 5 * * *'
];
const invalidStrings = ['invalid', '*', '* *', '* * *', '* * * *'];
test('shows id input', () => {
const { getByPlaceholderText } = render(InputCron, { label: 'Cron', id: 'cron' });
const input = getByPlaceholderText('* * * * *');
expect(input).toBeInTheDocument();
expect(input).toHaveAttribute('type', 'text');
});
test('state', async () => {
const { component, getByPlaceholderText } = render(InputCron, {
value: '',
label: 'Cron',
id: 'cron'
});
const input = getByPlaceholderText('* * * * *');
expect(component.value).toEqual('');
await userEvent.type(input, 'lorem');
expect(component.value).toEqual('lorem');
});
validStrings.forEach((validString) => {
test(`validates ${validString} as valid`, () => {
const { getByPlaceholderText } = render(InputCron, {
value: validString,
label: 'Cron',
id: 'cron'
});
const input = getByPlaceholderText('* * * * *') as HTMLInputElement;
expect(input.checkValidity()).toBe(true);
});
});
invalidStrings.forEach((invalidString) => {
test(`validates ${invalidString} as invalid`, () => {
const { getByPlaceholderText } = render(InputCron, {
value: invalidString,
label: 'Cron',
id: 'cron'
});
const input = getByPlaceholderText('* * * * *') as HTMLInputElement;
expect(input.checkValidity()).toBe(false);
});
});
+170
View File
@@ -0,0 +1,170 @@
import {
calculateTime,
timeToSeconds,
timeToMinutes,
secsToUnit
} from '$lib/helpers/timeConversion';
describe('Calculate time', () => {
const tests: Array<[number, string]> = [
[0.5, '500ms'],
[1, '1s'],
[1.5, '1s'],
[59, '59s'],
[60, '1m'],
[61, '1m'],
[119, '1m'],
[120, '2m'],
[121, '2m'],
[3599, '59m'],
[3600, '1h'],
[3601, '1h'],
[7199, '1h'],
[7200, '2h'],
[7201, '2h'],
[86399, '23h'],
[86400, '1d'],
[86401, '1d'],
[172799, '1d'],
[172800, '2d'],
[172801, '2d'],
[2591999, '29d'],
[2592000, '1M'],
[2592001, '1M'],
[31103999, '11M'],
[31104000, '1y'],
[31104001, '1y']
];
tests.forEach(([input, expected]) => {
it(`should return ${expected} for ${input}`, () => {
expect(calculateTime(input)).toBe(expected);
});
});
});
describe('Time to seconds', () => {
const tests: Array<{
args: Parameters<typeof timeToSeconds>;
expected: ReturnType<typeof timeToSeconds>;
}> = [
// ms
{ args: [0, 'ms'], expected: 0 },
{ args: [1000, 'ms'], expected: 1 },
{ args: [1000 * 60, 'ms'], expected: 60 },
// seconds
{ args: [0, 's'], expected: 0 },
{ args: [1, 's'], expected: 1 },
{ args: [60, 's'], expected: 60 },
// minutes
{ args: [0, 'm'], expected: 0 },
{ args: [1, 'm'], expected: 60 },
{ args: [60, 'm'], expected: 60 * 60 },
// hours
{ args: [0, 'h'], expected: 0 },
{ args: [1, 'h'], expected: 60 * 60 },
{ args: [24, 'h'], expected: 60 * 60 * 24 },
// days
{ args: [0, 'd'], expected: 0 },
{ args: [1, 'd'], expected: 60 * 60 * 24 },
{ args: [30, 'd'], expected: 60 * 60 * 24 * 30 },
// months
{ args: [0, 'M'], expected: 0 },
{ args: [1, 'M'], expected: 60 * 60 * 24 * 30 },
{ args: [12, 'M'], expected: 60 * 60 * 24 * 30 * 12 },
// years
{ args: [0, 'y'], expected: 0 },
{ args: [1, 'y'], expected: 60 * 60 * 24 * 30 * 12 },
{ args: [10, 'y'], expected: 60 * 60 * 24 * 30 * 12 * 10 }
];
tests.forEach(({ args, expected }) => {
it(`should return ${expected} for ${args}`, () => {
expect(timeToSeconds(...args)).toBe(expected);
});
});
});
describe('Time to minutes', () => {
const tests: Array<{
args: Parameters<typeof timeToMinutes>;
expected: ReturnType<typeof timeToMinutes>;
}> = [
// ms
{ args: [0, 'ms'], expected: 0 },
{ args: [1000 * 60, 'ms'], expected: 1 },
{ args: [1000 * 60 * 60, 'ms'], expected: 60 },
// seconds
{ args: [0, 's'], expected: 0 },
{ args: [60, 's'], expected: 1 },
{ args: [60 * 60, 's'], expected: 60 },
// minutes
{ args: [0, 'm'], expected: 0 },
{ args: [60, 'm'], expected: 60 },
{ args: [60 * 24, 'm'], expected: 60 * 24 },
// hours
{ args: [0, 'h'], expected: 0 },
{ args: [1, 'h'], expected: 60 },
{ args: [24, 'h'], expected: 60 * 24 },
// days
{ args: [0, 'd'], expected: 0 },
{ args: [1, 'd'], expected: 60 * 24 },
{ args: [30, 'd'], expected: 60 * 24 * 30 },
// months
{ args: [0, 'M'], expected: 0 },
{ args: [1, 'M'], expected: 60 * 24 * 30 },
{ args: [12, 'M'], expected: 60 * 24 * 30 * 12 },
// years
{ args: [0, 'y'], expected: 0 },
{ args: [1, 'y'], expected: 60 * 24 * 30 * 12 },
{ args: [2, 'y'], expected: 60 * 24 * 30 * 12 * 2 }
];
tests.forEach(({ args, expected }) => {
it(`should return ${expected} for ${args}`, () => {
expect(timeToMinutes(...args)).toBe(expected);
});
});
});
describe('Seconds to unit', () => {
const tests: Array<{
args: Parameters<typeof secsToUnit>;
expected: ReturnType<typeof secsToUnit>;
}> = [
// ms
{ args: [0, 'ms'], expected: 0 },
{ args: [1, 'ms'], expected: 1000 },
{ args: [60, 'ms'], expected: 60 * 1000 },
// seconds
{ args: [0, 's'], expected: 0 },
{ args: [1, 's'], expected: 1 },
{ args: [60, 's'], expected: 60 },
// minutes
{ args: [0, 'm'], expected: 0 },
{ args: [1, 'm'], expected: 1 / 60 },
{ args: [60, 'm'], expected: 1 },
// hours
{ args: [0, 'h'], expected: 0 },
{ args: [1, 'h'], expected: 1 / 60 / 60 },
{ args: [60 * 60, 'h'], expected: 1 },
// days
{ args: [0, 'd'], expected: 0 },
{ args: [1, 'd'], expected: 1 / 60 / 60 / 24 },
{ args: [60 * 60 * 24, 'd'], expected: 1 },
// months
{ args: [0, 'M'], expected: 0 },
{ args: [1, 'M'], expected: 1 / 60 / 60 / 24 / 30 },
{ args: [60 * 60 * 24 * 30, 'M'], expected: 1 },
// years
{ args: [0, 'y'], expected: 0 },
{ args: [1, 'y'], expected: 1 / 60 / 60 / 24 / 30 / 12 },
{ args: [60 * 60 * 24 * 30 * 12, 'y'], expected: 1 }
];
tests.forEach(({ args, expected }) => {
it(`should return ${expected} for ${args}`, () => {
expect(secsToUnit(...args)).toBe(expected);
});
});
});