mirror of
https://github.com/hashicorp/vault.git
synced 2026-05-28 04:10:44 -04:00
UI: Use timestamp.now() in custom messages (#29525)
* use timestamp.now() in custom messages * dynamically assert timezone * dynamically assert timezone * final cleanup, add comments * remove assertion count
This commit is contained in:
parent
4051cb4d4c
commit
9e6b5cebd1
3 changed files with 16 additions and 8 deletions
|
|
@ -3,6 +3,7 @@
|
|||
* SPDX-License-Identifier: BUSL-1.1
|
||||
*/
|
||||
import Model, { attr } from '@ember-data/model';
|
||||
import timestamp from 'core/utils/timestamp';
|
||||
import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';
|
||||
import { isAfter, addDays, startOfDay, parseISO, isBefore } from 'date-fns';
|
||||
import { withModelValidations } from 'vault/decorators/model-validations';
|
||||
|
|
@ -109,7 +110,9 @@ export default class MessageModel extends Model {
|
|||
editType: 'dateTimeLocal',
|
||||
label: 'Message starts',
|
||||
subText: 'Defaults to 12:00 a.m. the following day (local timezone).',
|
||||
defaultValue: addDays(startOfDay(new Date()), 1).toISOString(),
|
||||
defaultValue() {
|
||||
return addDays(startOfDay(timestamp.now()), 1).toISOString();
|
||||
},
|
||||
})
|
||||
startTime;
|
||||
@attr('dateTimeLocal', { editType: 'yield', label: 'Message expires' }) endTime;
|
||||
|
|
@ -126,7 +129,7 @@ export default class MessageModel extends Model {
|
|||
|
||||
// date helpers
|
||||
get isStartTimeAfterToday() {
|
||||
return isAfter(parseISO(this.startTime), new Date());
|
||||
return isAfter(parseISO(this.startTime), timestamp.now());
|
||||
}
|
||||
|
||||
// capabilities
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import { service } from '@ember/service';
|
|||
import { action } from '@ember/object';
|
||||
import Ember from 'ember';
|
||||
import { isAfter } from 'date-fns';
|
||||
import timestamp from 'core/utils/timestamp';
|
||||
|
||||
/**
|
||||
* @module Page::CreateAndEditMessageForm
|
||||
|
|
@ -63,7 +64,7 @@ export default class MessagesList extends Component {
|
|||
const modalMessages = this.args.messages?.filter((message) => message.type === 'modal') || [];
|
||||
const hasExpiredModalMessages = modalMessages.every((message) => {
|
||||
if (!message.endTime) return false;
|
||||
return isAfter(new Date(), new Date(message.endTime));
|
||||
return isAfter(timestamp.now(), new Date(message.endTime));
|
||||
});
|
||||
|
||||
if (!hasExpiredModalMessages && this.args.hasSomeActiveModals && this.args.message.type === 'modal') {
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import { format, addDays, startOfDay } from 'date-fns';
|
|||
import { CUSTOM_MESSAGES } from 'vault/tests/helpers/config-ui/message-selectors';
|
||||
import timestamp from 'core/utils/timestamp';
|
||||
import { GENERAL } from 'vault/tests/helpers/general-selectors';
|
||||
import sinon from 'sinon';
|
||||
|
||||
module('Integration | Component | messages/page/create-and-edit', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
|
@ -21,14 +22,14 @@ module('Integration | Component | messages/page/create-and-edit', function (hook
|
|||
setupMirage(hooks);
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
const now = new Date('2023-07-02T00:00:00Z'); // stub "now" for testing
|
||||
sinon.replace(timestamp, 'now', sinon.fake.returns(now));
|
||||
this.context = { owner: this.engine };
|
||||
this.store = this.owner.lookup('service:store');
|
||||
this.message = this.store.createRecord('config-ui/message');
|
||||
});
|
||||
|
||||
test('it should display all the create form fields and default radio button values', async function (assert) {
|
||||
assert.expect(17);
|
||||
|
||||
await render(hbs`<Messages::Page::CreateAndEdit @message={{this.message}} />`, {
|
||||
owner: this.engine,
|
||||
});
|
||||
|
|
@ -46,11 +47,14 @@ module('Integration | Component | messages/page/create-and-edit', function (hook
|
|||
assert.dom(CUSTOM_MESSAGES.field('message')).exists();
|
||||
assert.dom('[data-test-kv-key="0"]').exists();
|
||||
assert.dom('[data-test-kv-value="0"]').exists();
|
||||
assert.dom(CUSTOM_MESSAGES.input('startTime')).exists();
|
||||
assert
|
||||
.dom(CUSTOM_MESSAGES.input('startTime'))
|
||||
.hasValue(format(addDays(startOfDay(timestamp.now()), 1), datetimeLocalStringFormat));
|
||||
assert.dom(CUSTOM_MESSAGES.input('endTime')).exists();
|
||||
.hasValue(
|
||||
format(addDays(startOfDay(timestamp.now()), 1), datetimeLocalStringFormat),
|
||||
`message startTime defaults to midnight of following day. test context startTime: ${
|
||||
this.message.startTime
|
||||
}, now: ${timestamp.now().toISOString()}`
|
||||
);
|
||||
assert.dom(CUSTOM_MESSAGES.input('endTime')).hasValue('');
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue