diff --git a/ui/src/components/Terraform/TerraformListTable.tsx b/ui/src/components/Terraform/TerraformListTable.tsx index 5355a34645..41cc82c2b6 100644 --- a/ui/src/components/Terraform/TerraformListTable.tsx +++ b/ui/src/components/Terraform/TerraformListTable.tsx @@ -21,8 +21,15 @@ type Props = { export const getLastApplied = (tf: TerraformObject) => { const timestamp = _.find(tf?.conditions, { type: 'Apply' })?.timestamp; - if (!timestamp) return '-'; - return new Date(timestamp).toISOString(); + if (!timestamp) { + return '-'; + } + + try { + return new Date(timestamp).toISOString(); + } catch (e) { + return '-'; + } }; function TerraformListTable({ className, rows }: Props) { diff --git a/ui/src/components/Terraform/__tests__/TerraformListTable.tsx b/ui/src/components/Terraform/__tests__/TerraformListTable.tsx new file mode 100644 index 0000000000..f50b2c9c5f --- /dev/null +++ b/ui/src/components/Terraform/__tests__/TerraformListTable.tsx @@ -0,0 +1,62 @@ +import { TerraformObject } from '../../../api/terraform/types.pb'; +import { getLastApplied } from '../TerraformListTable'; + +describe('getLastApplied', () => { + const obj: TerraformObject = { + conditions: [ + { + type: 'Apply', + status: 'True', + timestamp: '2021-08-11T14:23:39Z', + }, + ], + }; + + it('should return the timestamp', () => { + expect(getLastApplied(obj)).toBe('2021-08-11T14:23:39.000Z'); + }); + + it('should return "-" if no timestamp', () => { + const obj2: TerraformObject = { + conditions: [ + { + type: 'Apply', + status: 'True', + }, + ], + }; + expect(getLastApplied(obj2)).toBe('-'); + }); + + it('should return "-" if no conditions', () => { + const obj3: TerraformObject = {}; + expect(getLastApplied(obj3)).toBe('-'); + }); + + it('should return "-" if no conditions with type Apply', () => { + const obj4: TerraformObject = { + conditions: [ + { + type: 'Apply1', + status: 'True', + timestamp: '2021-08-11T14:23:39Z', + }, + ], + }; + expect(getLastApplied(obj4)).toBe('-'); + }); + + it('should return "-" if the timestamp is not valid', () => { + const obj5: TerraformObject = { + conditions: [ + { + type: 'Apply', + status: 'True', + timestamp: 'foo', + }, + ], + }; + + expect(getLastApplied(obj5)).toBe('-'); + }); +});