Skip to content

Commit

Permalink
refactor: replace momentjs in desktop (#8959)
Browse files Browse the repository at this point in the history
Co-authored-by: Jennifer Shehane <[email protected]>
  • Loading branch information
gvdp and jennifer-shehane authored Oct 26, 2020
1 parent eb3fb4a commit bbc1368
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 60 deletions.
29 changes: 14 additions & 15 deletions packages/desktop-gui/cypress/integration/runs_list_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const moment = require('moment')

describe('Runs List', function () {
beforeEach(function () {
cy.fixture('user').as('user')
Expand Down Expand Up @@ -72,7 +70,7 @@ describe('Runs List', function () {

context('api server connection', function () {
beforeEach(function () {
const timestamp = moment('2016-12-19T10:00:00').valueOf()
const timestamp = new Date(2016, 11, 19, 10, 0, 0).valueOf()

cy.clock(timestamp)
this.getCurrentUser.resolve(this.user)
Expand Down Expand Up @@ -100,11 +98,12 @@ describe('Runs List', function () {
context('displays each run\'s data', function () {
beforeEach(function () {
cy.get('.runs-container li').first().as('firstRunRow')
cy.get('.runs-container li').eq(1).as('runRow')
cy.get('.runs-container li').eq(1).as('secondRunRow')
cy.get('.runs-container li').eq(3).as('fourthRunRow')
})

it('displays build num', function () {
cy.get('@runRow').contains(`#${this.runs[1].buildNumber}`)
cy.get('@secondRunRow').contains(`#${this.runs[1].buildNumber}`)
})

it('displays commit info', function () {
Expand All @@ -113,14 +112,14 @@ describe('Runs List', function () {
})

it('display no info msg & does not display avatar', () => {
cy.get('@runRow').within(function () {
cy.get('@secondRunRow').within(function () {
cy.get('.user-avatar').should('not.exist')
cy.contains('No commit info found')
})
})

it('displays platform info', () => {
cy.get('@runRow').within(function () {
cy.get('@secondRunRow').within(function () {
cy.contains(this.runs[1].instances[0].platform.osVersionFormatted)
cy.contains(this.runs[1].instances[0].platform.browserName)
cy.get('.fa-apple')
Expand All @@ -138,24 +137,24 @@ describe('Runs List', function () {
})

it('displays totals', function () {
cy.get('@runRow').contains(this.runs[1].totalFailed)
cy.get('@runRow').contains(this.runs[1].totalPassed)
cy.get('@secondRunRow').contains(this.runs[1].totalFailed)
cy.get('@secondRunRow').contains(this.runs[1].totalPassed)
})

it('displays times', function () {
cy.get('@runRow').contains('a few secs ago')
cy.get('@runRow').contains('00:16')
cy.get('@secondRunRow').contains('a few secs ago')
cy.get('@secondRunRow').contains('00:16')
})

it('displays separate timers for incomplete runs', function () {
cy.get('@firstRunRow').contains('24:47')
cy.get('.runs-container li').eq(3).contains('45:47')
cy.get('@firstRunRow').contains('12:24:47')
cy.get('@fourthRunRow').contains('12:45:47')
.then(() => {
cy.tick(1000)
})

cy.get('@firstRunRow').contains('24:48')
cy.get('.runs-container li').eq(3).contains('45:48')
cy.get('@firstRunRow').contains('12:24:48')
cy.get('@fourthRunRow').contains('12:45:48')
})

context('spec display', function () {
Expand Down
11 changes: 11 additions & 0 deletions packages/desktop-gui/cypress/integration/utils_spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import dayjs from 'dayjs'

const utils = require('../../src/lib/utils')

describe('durationFormatted', function () {
Expand Down Expand Up @@ -26,6 +28,15 @@ describe('durationFormatted', function () {
})
})

describe('getFormattedTimeFromNow', function () {
it('displays time a minute ago', () => {
const now = dayjs()
const dateInPast = now.subtract(1, 'minute').toISOString()

expect(utils.getFormattedTimeFromNow(dateInPast)).to.eq('a min ago')
})
})

describe('stripLeadingCyDirs', function () {
it('strips leading cypress directories from spec', function () {
expect(utils.stripLeadingCyDirs('cypress/integration/login_spec.js')).to.eq('login_spec.js')
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop-gui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cross-env": "6.0.3",
"cypress-multi-reporters": "1.4.0",
"cypress-react-unit-test": "4.11.2",
"dayjs": "^1.9.3",
"fira": "cypress-io/fira#fb63362742eea8cdce0d90825ab9264d77719e3d",
"gravatar": "1.8.0",
"human-interval": "1.0.0",
Expand All @@ -36,7 +37,6 @@
"mobx": "5.15.4",
"mobx-react": "6.1.8",
"mobx-react-devtools": "6.1.1",
"moment": "2.27.0",
"prop-types": "15.7.2",
"rc-collapse": "2.0.0",
"react": "16.8.6",
Expand Down
10 changes: 5 additions & 5 deletions packages/desktop-gui/src/duration-timer/duration-timer-store.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { observable, computed, action } from 'mobx'
import moment from 'moment'

import Timer from './duration-timer-model'
import dayjs from 'dayjs'

class DurationTimer {
class TimerStore {
@observable isRunning = false
@observable timer
@observable startTime

constructor (startTime) {
this.timer = new Timer()
this.startTime = moment(startTime)
this.startTime = dayjs(startTime)
}

@computed get mainDisplay () {
Expand All @@ -20,7 +20,7 @@ class DurationTimer {
@action measure () {
if (!this.isRunning) return

this.timer.milliseconds = moment().diff(this.startTime)
this.timer.milliseconds = dayjs().diff(this.startTime)

this.timerId = setTimeout(() => {
return this.measure()
Expand All @@ -41,4 +41,4 @@ class DurationTimer {
}
}

export default DurationTimer
export default TimerStore
24 changes: 0 additions & 24 deletions packages/desktop-gui/src/lib/configure-moment.js

This file was deleted.

51 changes: 49 additions & 2 deletions packages/desktop-gui/src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import _ from 'lodash'
import gravatar from 'gravatar'
import duration from '../../../server/lib/util/duration'
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import updateLocale from 'dayjs/plugin/updateLocale'
import duration from 'dayjs/plugin/duration'

dayjs.extend(duration)
dayjs.extend(relativeTime)
dayjs.extend(updateLocale)
dayjs.updateLocale('en', {
relativeTime: {
future: 'in %s',
past: '%s ago',
s: 'a few secs',
ss: '%d secs',
m: 'a min',
mm: '%d mins',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years',
},
})

const cyDirRegex = /^cypress\/integration\//g

Expand All @@ -10,7 +35,29 @@ const osIconLookup = {
linux: 'linux',
}

export const durationFormatted = duration.format
export const getFormattedTimeFromNow = (timeAsIsoString) => {
return dayjs(timeAsIsoString).fromNow()
}

export const durationFormatted = (durationInMs) => {
if (durationInMs < 1000) {
return `${durationInMs}ms`
}

const dayDuration = dayjs.duration(durationInMs)

const durationSecs = dayDuration.seconds() ? `${dayDuration.seconds()}` : ''
const durationMins = dayDuration.minutes() ? `${dayDuration.minutes()}` : ''
const durationHrs = dayDuration.hours() ? `${dayDuration.hours()}` : ''

const total = _.compact([
durationHrs,
_.padStart(durationMins, 2, '0'),
_.padStart(durationSecs, 2, '0'),
])

return total.join(':')
}

export const osIcon = (osName) => {
if (!osName) return ''
Expand Down
2 changes: 0 additions & 2 deletions packages/desktop-gui/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { render } from 'react-dom'

import ipc from './lib/ipc'
import handleGlobalErrors from './lib/handle-global-errors'
import momentOverrides from './lib/configure-moment'

import App from './app/app'

configureMobx({ enforceActions: 'observed' })

handleGlobalErrors()
momentOverrides()

if (window.env === 'test' || window.env === 'development') {
window.toJS = toJS
Expand Down
25 changes: 17 additions & 8 deletions packages/desktop-gui/src/runs/runs-list-item.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import _ from 'lodash'
import moment from 'moment'
import React, { Component } from 'react'
import Tooltip from '@cypress/react-tooltip'
import { BrowserIcon } from '@packages/ui-components'

import TimerDisplay from '../duration-timer/TimerDisplay'
import { osIcon, gravatarUrl, getStatusIcon, durationFormatted, browserNameFormatted, browserVersionFormatted, stripLeadingCyDirs, stripSharedDirsFromDir2 } from '../lib/utils'

import {
browserNameFormatted,
browserVersionFormatted,
durationFormatted,
getFormattedTimeFromNow,
getStatusIcon,
gravatarUrl,
osIcon,
stripLeadingCyDirs,
stripSharedDirsFromDir2,
} from '../lib/utils'

const RunDuration = ({ run }) => {
// Run was blocked due to exceeding limit
Expand Down Expand Up @@ -37,7 +46,7 @@ const RunDuration = ({ run }) => {
// Run is still going
if (run.createdAt) {
return (
<TimerDisplay startTime={run.createdAt} />
<TimerDisplay startTime={run.createdAt}/>
)
}

Expand Down Expand Up @@ -66,7 +75,7 @@ export default class RunsListItem extends Component {
<div className='row-column-wrapper'>
<div className='td-top-padding'>
<div>
{ run.commit && run.commit.branch ?
{run.commit && run.commit.branch ?
<span>
{run.commit.branch}
{this._displaySpec() ? ' / ' : null}
Expand Down Expand Up @@ -108,12 +117,12 @@ export default class RunsListItem extends Component {
<div className='row-column-wrapper'>
<div>
<i className='far fa-clock'></i>{' '}
{moment(run.createdAt).fromNow() === '1 secs ago' ? '1 sec ago' : moment(run.createdAt).fromNow()}
{getFormattedTimeFromNow(run.createdAt)}
</div>
</div>
<div className='row-column-wrapper'>
<div className='td-padding'>
<RunDuration run={run} />
<RunDuration run={run}/>
</div>
</div>
<div className='row-column-wrapper env-data'>
Expand Down Expand Up @@ -253,7 +262,7 @@ export default class RunsListItem extends Component {
_browserIcon = () => {
const browserName = _.get(this.props.run, 'instances[0].platform.browserName', '')

return browserName ? <BrowserIcon browserName={browserName} /> : null
return browserName ? <BrowserIcon browserName={browserName}/> : null
}

_browserDisplay = () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/desktop-gui/src/runs/runs-store.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import _ from 'lodash'
import { observable, action } from 'mobx'
import moment from 'moment'
import { action, observable } from 'mobx'

import Run from './run-model'
import dayjs from 'dayjs'

export class RunsStore {
@observable runs = []
Expand All @@ -21,7 +21,7 @@ export class RunsStore {
return new Run(run)
})

this.lastUpdated = moment().format('h:mm:ssa')
this.lastUpdated = dayjs().format('h:mm:ssa')
this.error = null
this.isLoading = false
this.isLoaded = true
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12031,6 +12031,11 @@ daty@^1.0.7:
diff-dates "^1.0.0"
formatoid "^1.0.0"

dayjs@^1.9.3:
version "1.9.3"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.9.3.tgz#b7f94b22ad2a136a4ca02a01ab68ae893fe1a268"
integrity sha512-V+1SyIvkS+HmNbN1G7A9+ERbFTV9KTXu6Oor98v2xHmzzpp52OIJhQuJSTywWuBY5pyAEmlwbCi1Me87n/SLOw==

days@^1.0.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/days/-/days-1.1.1.tgz#b039d1e5ad5335ca689401855546f02d4cfcb362"
Expand Down

4 comments on commit bbc1368

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bbc1368 Oct 26, 2020

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/5.4.1/circle-develop-bbc13689a827b4950aba3eeae8fe46affe517142/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bbc1368 Oct 26, 2020

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/5.4.1/appveyor-develop-bbc13689a827b4950aba3eeae8fe46affe517142/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bbc1368 Oct 26, 2020

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/5.4.1/appveyor-develop-bbc13689a827b4950aba3eeae8fe46affe517142/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bbc1368 Oct 26, 2020

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/5.4.1/circle-develop-bbc13689a827b4950aba3eeae8fe46affe517142/cypress.tgz

Please sign in to comment.