Skip to content

Commit

Permalink
fix: add support for SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Mar 27, 2023
1 parent 95906c3 commit 47080ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
21 changes: 16 additions & 5 deletions src/helpers/DateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,24 @@ export default class DateHelper {
constructor() {
this.locale = DEFAULT_LOCALE;
this.timezone = dayjs.tz.guess();
(window as any).dayjs ||= dayjs;
if (typeof window !== undefined) {
(window as any).dayjs ||= dayjs;
}
}

async setup({ options }: { options: OptionsType }) {
this.timezone = options.date.timezone || dayjs.tz.guess();
const userLocale = options.date.locale;

if (typeof userLocale === 'string' && userLocale !== DEFAULT_LOCALE) {
const locale =
(window as any)[`dayjs_locale_${userLocale}`] ||
(await this.loadLocale(userLocale));
let locale;
if (typeof window !== undefined) {
locale =
(window as any)[`dayjs_locale_${userLocale}`] ||
(await this.loadBrowserLocale(userLocale));
} else {
locale = await this.loadNodeLocale(userLocale)
}
dayjs.locale(userLocale);
this.locale = locale;
}
Expand Down Expand Up @@ -217,7 +224,7 @@ export default class DateHelper {

// this function will work cross-browser for loading scripts asynchronously
// eslint-disable-next-line class-methods-use-this
loadLocale(userLocale: string): Promise<any> {
loadBrowserLocale(userLocale: string): Promise<any> {
return new Promise((resolve, reject) => {
const s = document.createElement('script');
s.type = 'text/javascript';
Expand All @@ -232,4 +239,8 @@ export default class DateHelper {
document.head.appendChild(s);
});
}

loadNodeLocale(userLocale: string): Promise<any> {
return import(`dayjs/locale/${userLocale}`);
}
}
24 changes: 13 additions & 11 deletions test/helpers/DateHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('DateHelper', () => {
let options: Options;
let dateHelper: DateHelper;

const loadLocaleMock = jest
.spyOn(DateHelper.prototype, 'loadLocale')
const loadBrowserLocaleMock = jest
.spyOn(DateHelper.prototype, 'loadBrowserLocale')
.mockImplementation(() => Promise.resolve(locale_fr));

beforeEach(async () => {
Expand All @@ -31,27 +31,29 @@ describe('DateHelper', () => {
options.init({ date: { locale: 'en' } });
await dateHelper.setup(options);
expect(dateHelper.date().locale()).toEqual('en');
expect(loadLocaleMock).not.toHaveBeenCalled();
expect(loadBrowserLocaleMock).not.toHaveBeenCalled();
expect(dateHelper.locale).toBe('en');
});
});

describe('when using no locale value', () => {
it('instantiate dayjs with default EN locale', () => {
expect(loadLocaleMock).not.toHaveBeenCalled();
expect(loadBrowserLocaleMock).not.toHaveBeenCalled();
expect(dateHelper.date().locale()).toEqual('en');
expect(dateHelper.locale).toBe('en');
});
});

describe('when using locale FR', () => {
it('instantiate dayjs with the FR locale', async () => {
options.init({ date: { locale: 'fr' } });
await dateHelper.setup(options);
expect(loadLocaleMock).toHaveBeenCalledTimes(1);
expect(dateHelper.date().locale()).toEqual('fr');
expect(dateHelper.locale).toHaveProperty('name');
});
describe('on browser env', () => {
it('instantiate dayjs with the FR locale', async () => {
options.init({ date: { locale: 'fr' } });
await dateHelper.setup(options);
expect(loadBrowserLocaleMock).toHaveBeenCalledTimes(1);
expect(dateHelper.date().locale()).toEqual('fr');
expect(dateHelper.locale).toHaveProperty('name');
});
})
});
});

Expand Down

0 comments on commit 47080ec

Please sign in to comment.