Skip to content

Commit

Permalink
Merge pull request #59 from ofuton/fix-fromNow-bug
Browse files Browse the repository at this point in the history
Fix bug that argument passed to fromNow() is wrong
  • Loading branch information
emiksk committed Jul 3, 2023
2 parents 5fb7de5 + d6a2f9c commit 5ade0bd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "maimodorun",
"private": true,
"version": "2.0.0",
"version": "2.0.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
6 changes: 3 additions & 3 deletions src/popup/component/Histories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const RecordCard: React.FC<RecordProps> = ({ record, clickTrashButtonHandler, re
</div>
<div className="l-histories-list-card-right-bottom-right">
<div className="l-histories-list-card-right-bottom-right-time">
{fromNow(new Date())}
{fromNow(record.timestamp)}
</div>
</div>
</div>
Expand Down Expand Up @@ -192,6 +192,6 @@ const RecordCardTag: React.FC<RecordCardTagProps> = ({ scope }) => {
)
}

const fromNow = (date: Date): string => {
return dayjs(date).fromNow()
const fromNow = (timestamp: number): string => {
return dayjs.unix(timestamp).fromNow()
}
74 changes: 73 additions & 1 deletion tests/popup/component/Histories.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import { Histories } from '../../../src/popup/component/Histories.tsx'
import userEvent from '@testing-library/user-event'
import dayjs from 'dayjs'

describe('src/popup/component/Histories.tsx', () => {
afterEach(() => {
Expand Down Expand Up @@ -128,4 +129,75 @@ describe('src/popup/component/Histories.tsx', () => {

expect(screen.getByText('tags will be removed')).toBeInTheDocument()
})

describe('fromNow()', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
// restoring date after each test run
vi.useRealTimers()
})

it('数秒前', () => {
const now = 1234567890
vi.setSystemTime(dayjs.unix(now).toDate())
const props = {
histories: [{
scope: 'thread.root',
url: 'https://example.com/1',
title: 'Example Title',
iconUrl: 'https://example.com/icon',
timestamp: now - 5,
content: '<span>tags will be removed</span>'
}],
clickTrashButtonHandler: async (_: string) => {},
recoveryRecordHandler: async (_: string) => {}
}
render(<Histories {...props} />)

expect(screen.getByText('数秒前')).toBeInTheDocument()
})

it('5分前', () => {
const now = 1234567890
vi.setSystemTime(dayjs.unix(now).toDate())
const props = {
histories: [{
scope: 'thread.root',
url: 'https://example.com/1',
title: 'Example Title',
iconUrl: 'https://example.com/icon',
timestamp: now - 5 * 60,
content: '<span>tags will be removed</span>'
}],
clickTrashButtonHandler: async (_: string) => {},
recoveryRecordHandler: async (_: string) => {}
}
render(<Histories {...props} />)

expect(screen.getByText('5分前')).toBeInTheDocument()
})

it('1日前', () => {
const now = 1234567890
vi.setSystemTime(dayjs.unix(now).toDate())
const props = {
histories: [{
scope: 'thread.root',
url: 'https://example.com/1',
title: 'Example Title',
iconUrl: 'https://example.com/icon',
timestamp: now - 60 * 60 * 24,
content: '<span>tags will be removed</span>'
}],
clickTrashButtonHandler: async (_: string) => {},
recoveryRecordHandler: async (_: string) => {}
}
render(<Histories {...props} />)

expect(screen.getByText('1日前')).toBeInTheDocument()
})
})
})
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zipPack from 'vite-plugin-zip-pack'
const manifest = defineManifest({
manifest_version: 3,
name: 'Maimodorun',
version: '2.0.0',
version: '2.0.1',
description: 'maimodorun: Recovery Tool for Forms on kintone',
icons: {
16: 'assets/icon16.png',
Expand Down

0 comments on commit 5ade0bd

Please sign in to comment.