Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix field date test case #2057

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions plugins/field-date/src/field_date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @fileoverview Plugin overview.
*/
import * as Blockly from 'blockly/core';
import {getLocaleDateString} from './utils';

/**
* Class for a date input field.
Expand Down Expand Up @@ -167,26 +168,6 @@ export class FieldDate extends Blockly.FieldTextInput {
/* eslint-enable @typescript-eslint/naming-convention */
}

/**
* Get the string formatted locally to the user.
* @param dateString A string in the format 'yyyy-mm-dd'
* @returns the locale date string for the date.
*/
function getLocaleDateString(dateString: string): string {
// NOTE: `date.toLocaleDateString()` will be the day before for western dates
// due to an unspecified time & timezone assuming midnight at GMT+0.
const date = new Date(dateString);

// NOTE: This format varies per region.
// Ex: "5/12/2023", "12/05/2023", "12.5.2023", "2023/5/12", "१२/५/२०२३"
const language = navigator.language ?? 'en-US';
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options
return new Intl.DateTimeFormat(language, {
// Print the date for GMT+0 since the date object assumes midnight at GMT+0.
timeZone: 'UTC',
}).format(date);
}

/**
* NOTE: There are a few minor ways to tweak the datepicker CSS, though they're
* not consistent across browsers.
Expand Down
19 changes: 19 additions & 0 deletions plugins/field-date/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Get the string formatted locally to the user.
* @param dateString A string in the format 'yyyy-mm-dd'
* @returns the locale date string for the date.
*/
export function getLocaleDateString(dateString: string): string {
// NOTE: `date.toLocaleDateString()` will be the day before for western dates
// due to an unspecified time & timezone assuming midnight at GMT+0.
const date = new Date(dateString);

// NOTE: This format varies per region.
// Ex: "5/12/2023", "12/05/2023", "12.5.2023", "2023/5/12", "१२/५/२०२३"
const language = navigator.language ?? 'en-US';
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options
return new Intl.DateTimeFormat(language, {
// Print the date for GMT+0 since the date object assumes midnight at GMT+0.
timeZone: 'UTC',
}).format(date);
}
5 changes: 3 additions & 2 deletions plugins/field-date/test/field_date_test.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import {assert} from 'chai';
import {testHelpers} from '@blockly/dev-tools';
import {FieldDate} from '../src/index';
import {getLocaleDateString} from '../src/utils';

const {
assertFieldValue,
Expand All @@ -24,7 +25,7 @@ if (!global.navigator) {
};
}

suite.skip('FieldDate', function () {
suite('FieldDate', function () {
/**
* Configuration for field tests with invalid values.
* @type {Array<FieldCreationTestCase>}
Expand Down Expand Up @@ -106,7 +107,7 @@ suite.skip('FieldDate', function () {
});
suite('Value -> New Value', function () {
const initialValue = '2020-02-20';
const initialText = new Date(initialValue).toLocaleDateString();
const initialText = getLocaleDateString(initialValue);
setup(function () {
this.field = new FieldDate(initialValue);
});
Expand Down