-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from atlassian/ARC-2676-connected-state
Arc-2676 connected state
- Loading branch information
Showing
10 changed files
with
369 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/jenkins-for-jira-ui/src/components/ConnectionPanel/ConnectedJenkinsServers.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import moment from 'moment'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { ConnectedJenkinsServers, timeFromNow } from './ConnectedJenkinsServers'; | ||
import { EventType, JenkinsServer } from '../../../../src/common/types'; | ||
|
||
describe('timeFromNow util', () => { | ||
test('returns correct string for hours', () => { | ||
const date = moment().subtract(5, 'hours'); | ||
const result = timeFromNow(date); | ||
expect(result).toEqual('About 5 hours ago'); | ||
}); | ||
|
||
test('returns correct string for single hour', () => { | ||
const date = moment().subtract(1, 'hours'); | ||
const result = timeFromNow(date); | ||
expect(result).toEqual('About 1 hour ago'); | ||
}); | ||
|
||
test('returns correct string for minutes', () => { | ||
const date = moment().subtract(30, 'minutes'); | ||
const result = timeFromNow(date); | ||
expect(result).toEqual('About 30 minutes ago'); | ||
}); | ||
|
||
test('returns correct string for single minute', () => { | ||
const date = moment().subtract(1, 'minutes'); | ||
const result = timeFromNow(date); | ||
expect(result).toEqual('About 1 minute ago'); | ||
}); | ||
|
||
test('returns correct string for seconds', () => { | ||
const date = moment().subtract(45, 'seconds'); | ||
const result = timeFromNow(date); | ||
expect(result).toEqual('About 45 seconds ago'); | ||
}); | ||
|
||
test('returns correct string for single second', () => { | ||
const date = moment().subtract(1, 'seconds'); | ||
const result = timeFromNow(date); | ||
expect(result).toEqual('About 1 second ago'); | ||
}); | ||
}); | ||
|
||
describe('ConnectedJenkinsServers suite', () => { | ||
const mockConnectedJenkinsServer: JenkinsServer = { | ||
name: 'mockServer', | ||
uuid: '123', | ||
pipelines: [ | ||
{ | ||
name: 'mockPipeline', | ||
lastEventStatus: 'successful', | ||
lastEventType: EventType.DEPLOYMENT, | ||
lastEventDate: new Date() | ||
} | ||
] | ||
}; | ||
|
||
test('renders column headers', () => { | ||
render(<ConnectedJenkinsServers connectedJenkinsServer={mockConnectedJenkinsServer} />); | ||
expect(screen.getByText('Pipeline')).toBeInTheDocument(); | ||
expect(screen.getByText('Event')).toBeInTheDocument(); | ||
expect(screen.getByText('Received')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders correct job & event content', () => { | ||
render(<ConnectedJenkinsServers connectedJenkinsServer={mockConnectedJenkinsServer} />); | ||
expect(screen.getByText(mockConnectedJenkinsServer.pipelines[0].name)).toBeInTheDocument(); | ||
expect(screen.getByText('successful deployment')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders correct time content', () => { | ||
render(<ConnectedJenkinsServers connectedJenkinsServer={mockConnectedJenkinsServer} />); | ||
const timeContent = moment().diff(moment(new Date(mockConnectedJenkinsServer.pipelines[0].lastEventDate)), 'hours') < 24 | ||
? timeFromNow(new Date(mockConnectedJenkinsServer.pipelines[0].lastEventDate)) | ||
: moment(new Date(mockConnectedJenkinsServer.pipelines[0].lastEventDate)).format('Do MMMM YYYY [at] hh:mma'); | ||
expect(screen.getByText(timeContent)).toBeInTheDocument(); | ||
}); | ||
}); |
128 changes: 128 additions & 0 deletions
128
app/jenkins-for-jira-ui/src/components/ConnectionPanel/ConnectedJenkinsServers.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import React from 'react'; | ||
import moment from 'moment/moment'; | ||
import { cx } from '@emotion/css'; | ||
import DynamicTable from '@atlaskit/dynamic-table'; | ||
import { JenkinsPipeline, JenkinsServer } from '../../../../src/common/types'; | ||
import { | ||
mapLastEventStatus, | ||
mapLastEventStatusIcons | ||
} from '../JenkinsServerList/ConnectedServer/ConnectedServers'; | ||
import { | ||
connectedStateCell, | ||
connectedStateCellContainer, | ||
connectedStateCellEvent, | ||
connectedStateContainer | ||
} from './ConnectionPanel.styles'; | ||
|
||
export const timeFromNow = (date: moment.MomentInput): string => { | ||
const seconds = moment().diff(moment(date), 'seconds'); | ||
const minutes = moment().diff(moment(date), 'minutes'); | ||
const hours = moment().diff(moment(date), 'hours'); | ||
|
||
if (hours > 0) { | ||
return `About ${hours} hour${hours > 1 ? 's' : ''} ago`; | ||
} | ||
if (minutes > 0) { | ||
return `About ${minutes} minute${minutes > 1 ? 's' : ''} ago`; | ||
} | ||
return `About ${seconds} second${seconds > 1 ? 's' : ''} ago`; | ||
}; | ||
|
||
type ConnectedStateProps = { | ||
connectedJenkinsServer: JenkinsServer | ||
}; | ||
|
||
type TableHead = { | ||
cells: { | ||
key: string; | ||
content: string; | ||
}[]; | ||
}; | ||
|
||
interface Row { | ||
cells: { | ||
key: string; | ||
content: React.ReactNode; | ||
}[]; | ||
} | ||
|
||
const ConnectedJenkinsServers = ({ connectedJenkinsServer }: ConnectedStateProps): JSX.Element => { | ||
const tableHead = ():TableHead => { | ||
return { | ||
cells: [ | ||
{ | ||
key: 'job', | ||
content: 'Pipeline' | ||
}, | ||
{ | ||
key: 'event', | ||
content: 'Event' | ||
}, | ||
{ | ||
key: 'time', | ||
content: 'Received' | ||
} | ||
] | ||
}; | ||
}; | ||
|
||
const rows = (serverName: string, serverId: string, pipelines: JenkinsPipeline[] = []): Row[] => { | ||
return ( | ||
pipelines.map((pipeline: JenkinsPipeline) => ({ | ||
cells: [ | ||
{ | ||
key: 'job', | ||
content: ( | ||
<div className={cx(connectedStateCellContainer)}> | ||
<div className={cx(connectedStateCell)}> | ||
{pipeline.name} | ||
</div> | ||
</div> | ||
) | ||
}, | ||
{ | ||
key: 'event', | ||
content: ( | ||
<div className={cx(connectedStateCellContainer)}> | ||
<> | ||
<div className={cx(connectedStateCell)}> | ||
{mapLastEventStatusIcons(pipeline.lastEventStatus)} | ||
</div> | ||
</> | ||
<div className={cx(connectedStateCell, connectedStateCellEvent)}> | ||
{mapLastEventStatus(pipeline.lastEventStatus)} {pipeline.lastEventType} | ||
</div> | ||
</div> | ||
) | ||
}, | ||
{ | ||
key: 'time', | ||
content: ( | ||
<div className={cx(connectedStateCellContainer)}> | ||
<div className={cx(connectedStateCell)}> | ||
{ | ||
moment().diff(moment(new Date(pipeline.lastEventDate)), 'hours') < 24 | ||
? timeFromNow(new Date(pipeline.lastEventDate)) | ||
: moment(new Date(pipeline.lastEventDate)).format('Do MMMM YYYY [at] hh:mma') | ||
} | ||
</div> | ||
</div> | ||
) | ||
} | ||
] | ||
})) | ||
); | ||
}; | ||
|
||
return ( | ||
<div className={cx(connectedStateContainer)}> | ||
<DynamicTable | ||
head={tableHead()} | ||
rows={rows(connectedJenkinsServer.name, connectedJenkinsServer.uuid, connectedJenkinsServer.pipelines)} | ||
loadingSpinnerSize='large' | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export { ConnectedJenkinsServers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.