Skip to content

Commit 201a2d7

Browse files
fix: timing on bridge bugged in first minute (#383)
1 parent d2abf17 commit 201a2d7

File tree

8 files changed

+66
-78
lines changed

8 files changed

+66
-78
lines changed

jest.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export default {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
24
projects: ['<rootDir>/packages/*'],
35
};

packages/app-commons/package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"types": "./src/index.ts",
88
"exports": {
99
".": "./src/index.ts",
10-
"./src/chains/fuel": "./src/chains/fuel.ts"
10+
"./src/chains/fuel": "./src/chains/fuel.ts",
11+
"./src/utils/dayjs": "./src/utils/dayjs.ts"
1112
},
1213
"scripts": {
1314
"ts:check": "tsc --noEmit"
@@ -18,7 +19,8 @@
1819
"fuels": "0.86.0",
1920
"react": "18.2.0",
2021
"react-dom": "18.2.0",
21-
"tailwind-variants": "0.1.20"
22+
"tailwind-variants": "0.1.20",
23+
"dayjs": "1.11.10"
2224
},
2325
"devDependencies": {
2426
"@fuels/ui": "workspace:*",
@@ -29,6 +31,7 @@
2931
"react": "18.2.0",
3032
"react-dom": "18.2.0",
3133
"tailwind-variants": "0.1.20",
32-
"typescript": "5.4.5"
34+
"typescript": "5.4.5",
35+
"dayjs": "1.11.10"
3336
}
3437
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import dayjs from 'dayjs';
2+
import englishLocale from 'dayjs/locale/en.js';
3+
import relativeTime from 'dayjs/plugin/relativeTime';
4+
5+
export function createDayjs() {
6+
dayjs.extend(relativeTime, {});
7+
const localeObject = {
8+
...englishLocale,
9+
name: 'fuel',
10+
relativeTime: {
11+
future: 'in %s',
12+
past: '%s ago',
13+
s: 'less than a minute',
14+
m: 'a minute',
15+
mm: '%d minutes',
16+
h: 'an hour',
17+
hh: '%d hours',
18+
d: 'a day',
19+
dd: '%d days',
20+
M: 'a month',
21+
MM: '%d months',
22+
y: 'a year',
23+
yy: '%d years',
24+
},
25+
};
26+
27+
dayjs.locale('fuel', localeObject);
28+
return dayjs;
29+
}

packages/app-explorer/next.config.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { redirects } from './src/redirects.mjs';
2+
import path from "path";
23

34
/** @type {import('next').NextConfig} */
45
const config = {
@@ -90,7 +91,7 @@ const config = {
9091
);
9192
// Modify the file loader rule to ignore *.svg, since we have it handled now.
9293
fileLoaderRule.exclude = /\.svg$/i;
93-
94+
config.resolve.alias.dayjs = path.resolve('./node_modules/dayjs');
9495
return config;
9596
},
9697
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import dayjs from 'dayjs';
1+
import { createDayjs } from 'app-commons/src/utils/dayjs';
22
import relativeTime from 'dayjs/plugin/relativeTime';
33

4-
dayjs.extend(relativeTime, {
5-
rounding: (num) => Math.ceil(num),
6-
});
7-
84
export const distanceToNow = (fromDate: Date) => {
5+
const dayjs = createDayjs();
6+
dayjs.extend(relativeTime, {
7+
rounding: (num) => Math.ceil(num),
8+
});
99
return dayjs(fromDate).toNow(true);
1010
};

packages/e2e-tests/playwright-html/hard/index.html

-69
This file was deleted.

packages/tests/date.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { distanceToNow } from '../app-portal/src/systems/Chains/fuel/utils/date';
2+
3+
describe('date', () => {
4+
it('Should generate distance from now in less than a minute', () => {
5+
const actualDate = new Date('2024-06-19T15:00:01');
6+
const futureDate = new Date('2024-06-19T15:00:11');
7+
jest.useFakeTimers().setSystemTime(actualDate);
8+
expect(distanceToNow(futureDate)).toBe('less than a minute');
9+
jest.restoreAllMocks();
10+
});
11+
12+
it('Should generate distance from now in a minute', () => {
13+
const actualDate = new Date('2024-06-19T15:00:01');
14+
const futureDate = new Date('2024-06-19T15:01:11');
15+
jest.useFakeTimers().setSystemTime(actualDate);
16+
expect(distanceToNow(futureDate)).toBe('a minute');
17+
jest.restoreAllMocks();
18+
});
19+
});

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)