-
Notifications
You must be signed in to change notification settings - Fork 14
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
fix: Made timestamp consistant #46
Changes from 4 commits
ba05db8
5f02cc4
44fa52e
f52a0f0
ec1fc3c
48cb2e2
b613cef
4a2f23d
dea2d66
0a24856
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,19 @@ export function openActivityLogPage() { | |
url: getActivityLogPageURL(), | ||
}); | ||
} | ||
|
||
export function dateTimeFormat(timestamp, options) { | ||
const dateTime = new Date(timestamp); | ||
const time = dateTime.toLocaleTimeString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done :-) |
||
|
||
if (options?.timeOnly) { | ||
return time; | ||
} | ||
|
||
const dateFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' }; | ||
const date = new Intl.DateTimeFormat(undefined, dateFormatOptions).format( | ||
dateTime | ||
); | ||
|
||
return `${date} ${time}`; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,9 @@ export default class ExtensionMonitor { | |
|
||
createLogListener() { | ||
return async (details) => { | ||
// set a timestamp(number) as the value of timeStamp property | ||
details.timeStamp = Date.parse(details.timeStamp); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add the next comment:
|
||
|
||
this.logs.push(details); | ||
await this.sendLogs(details); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ActivityLog from '../src/lib/ext-activitylog'; | |
import { FilterOption } from '../src/lib/web-component/filter-option/filter-option-element'; | ||
import { LogView } from '../src/lib/web-component/log-view/log-view-element'; | ||
import { FilterKeyword } from '../src/lib/web-component/filter-keyword/filter-keyword-element'; | ||
import * as ExtListen from '../src/lib/ext-listen'; | ||
|
||
const activityLogHtml = fs.readFileSync( | ||
path.resolve(__dirname, '../src/activitylog/activitylog.html'), | ||
|
@@ -269,3 +270,77 @@ test('clearing logs from activitylog page', async () => { | |
expect(activityLog.model.logs).toMatchObject([]); | ||
expect(getTableRows().length).toBe(0); | ||
}); | ||
|
||
test('timestamp is formatted and rendered correctly', () => { | ||
const logTimestamp = 1597686226302; | ||
|
||
const logs = [ | ||
{ | ||
/* renders 1st row in table */ | ||
id: 'id1@test', | ||
viewType: 'viewType@test', | ||
type: 'type@test', | ||
data: [{ test: 'test1@data' }], | ||
timeStamp: logTimestamp, | ||
}, | ||
]; | ||
|
||
const addListener = jest.fn(); | ||
const sendMessage = jest.fn(); | ||
const connect = jest.fn(); | ||
|
||
window.browser = { | ||
runtime: { | ||
onMessage: { addListener }, | ||
sendMessage, | ||
connect, | ||
}, | ||
}; | ||
|
||
sendMessage.mockImplementation(() => { | ||
return Promise.resolve({ existingLogs: [] }); | ||
}); | ||
|
||
document.body.innerHTML = activityLogBody; | ||
const dateTimeFormatFn = jest.spyOn(ExtListen, 'dateTimeFormat'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't mock your function, but the API that you're using. The only changes should be:
By doing both, the generated time strings become fully deterministic / predictable, and you can put the expected times in strings below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done :-) Thanks |
||
|
||
const { activityLog } = new ActivityLog(); | ||
|
||
// To have consistant date format, we choose "en-US" formatting | ||
let expectedDate; | ||
dateTimeFormatFn.mockImplementation((timestamp, options) => { | ||
const dateTime = new Date(timestamp); | ||
const time = dateTime.toLocaleTimeString(); | ||
|
||
if (options?.timeOnly) { | ||
return time; | ||
} | ||
|
||
const dateFormatOptions = { | ||
month: 'short', | ||
day: 'numeric', | ||
year: 'numeric', | ||
}; | ||
expectedDate = new Intl.DateTimeFormat('en-US', dateFormatOptions).format( | ||
dateTime | ||
); | ||
|
||
return `${expectedDate} ${time}`; | ||
}); | ||
|
||
activityLog.handleNewLogs(logs); | ||
|
||
expect(dateTimeFormatFn).toHaveBeenCalled(); | ||
|
||
const dateTime = new Date(logTimestamp); | ||
const expectedTime = dateTime.toLocaleTimeString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard-code the expected strings instead of generating it based on the dynamic objects. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done :-) |
||
const expectedDateTime = `${expectedDate} ${expectedTime}`; | ||
|
||
const tableBody = activityLog.view.logView.shadowRoot.querySelector('tbody'); | ||
const tableRows = tableBody.querySelectorAll('tr'); | ||
const firstRowTimestamp = tableRows[0].querySelector('.timestamp'); | ||
|
||
// For log timestamp: 1597686226302 | ||
expect(firstRowTimestamp.textContent).toEqual(expectedTime); | ||
expect(firstRowTimestamp.title).toEqual(expectedDateTime); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you put this function in
ext-listen.js
instead of a new file called, say,formatters.js
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, made a new file
formatters.js