-
-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
314a08b
commit ee24ab7
Showing
4 changed files
with
118 additions
and
57 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
40 changes: 40 additions & 0 deletions
40
...tend/src/component/feature/FeatureView/FeatureEnvironmentSeen/getLatestLastSeenAt.test.ts
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,40 @@ | ||
import { IEnvironments, IFeatureEnvironment } from 'interfaces/featureToggle'; | ||
|
||
import { getLatestLastSeenAt } from './getLatestLastSeenAt'; | ||
|
||
describe('getLatestLastSeenAt', () => { | ||
test('should return the most recent lastSeenAt date', () => { | ||
const input: IEnvironments[] = [ | ||
{ | ||
name: 'test1', | ||
lastSeenAt: '2023-10-22T08:48:11.869Z', | ||
enabled: false, | ||
}, | ||
{ | ||
name: 'test2', | ||
lastSeenAt: '2023-10-23T08:48:11.869Z', | ||
enabled: true, | ||
}, | ||
{ | ||
name: 'test3', | ||
lastSeenAt: '2023-10-24T08:48:11.869Z', | ||
enabled: true, | ||
}, | ||
]; | ||
const expected = '2023-10-24T08:48:11.869Z'; | ||
expect(getLatestLastSeenAt(input)).toBe(expected); | ||
}); | ||
|
||
test('should handle an empty array', () => { | ||
const input: IEnvironments[] = []; | ||
const expected = null; | ||
expect(getLatestLastSeenAt(input)).toBe(expected); | ||
}); | ||
|
||
test('should not fail with non-standard date formats', () => { | ||
const input: IEnvironments[] = [ | ||
{ name: 'test', lastSeenAt: 'Some Invalid Date', enabled: true }, | ||
]; | ||
expect(() => getLatestLastSeenAt(input)).not.toThrow(); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
frontend/src/component/feature/FeatureView/FeatureEnvironmentSeen/getLatestLastSeenAt.ts
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,19 @@ | ||
import { IEnvironments, IFeatureEnvironment } from 'interfaces/featureToggle'; | ||
|
||
export const getLatestLastSeenAt = ( | ||
environments: IEnvironments[] | IFeatureEnvironment[], | ||
) => { | ||
try { | ||
if (!Array.isArray(environments) || environments.length === 0) { | ||
return null; | ||
} | ||
|
||
return environments | ||
.filter((item) => Boolean(item.lastSeenAt)) | ||
.map((item) => new Date(item.lastSeenAt!)) | ||
.reduce((latest, current) => (current > latest ? current : latest)) | ||
.toISOString(); | ||
} catch (error) { | ||
return null; | ||
} | ||
}; |
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