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

UI: Use timestamp.now() in custom messages #29525

Merged
merged 5 commits into from
Feb 7, 2025
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
7 changes: 5 additions & 2 deletions ui/app/models/config-ui/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ 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);
setupEngine(hooks, 'config-ui');
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);
drivera258 marked this conversation as resolved.
Show resolved Hide resolved

await render(hbs`<Messages::Page::CreateAndEdit @message={{this.message}} />`, {
owner: this.engine,
});
Expand All @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant 🧹

assert
.dom(CUSTOM_MESSAGES.input('startTime'))
.hasValue(format(addDays(startOfDay(timestamp.now()), 1), datetimeLocalStringFormat));
assert.dom(CUSTOM_MESSAGES.input('endTime')).exists();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant 🧹

.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('');
});

Expand Down
Loading