-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new VeeamCapacityOverviewRow component
- Loading branch information
1 parent
6026d7e
commit 0abcf6a
Showing
4 changed files
with
158 additions
and
46 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
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
src/react/ui-elements/Veeam/VeeamCapacityOverviewRow.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 { render, screen } from '@testing-library/react'; | ||
import { QueryClient, QueryClientProvider } from 'react-query'; | ||
import { VeeamCapacityOverviewRow } from './VeeamCapacityOverviewRow'; | ||
import { rest } from 'msw'; | ||
import { setupServer } from 'msw/node'; | ||
import { bucketName } from '../../../js/mock/S3Client'; | ||
import { NewWrapper, TEST_API_BASE_URL } from '../../utils/testUtil'; | ||
import { VEEAM_XML_PREFIX } from './VeeamConstants'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
describe('VeeamCapacityOverviewRow', () => { | ||
const server = setupServer( | ||
rest.get( | ||
`${TEST_API_BASE_URL}/${bucketName}/${VEEAM_XML_PREFIX}/capacity.xml`, | ||
(req, res, ctx) => { | ||
return res(ctx.status(200)); | ||
}, | ||
), | ||
); | ||
beforeAll(() => { | ||
server.listen({ onUnhandledRequest: 'error' }); | ||
}); | ||
afterEach(() => { | ||
server.resetHandlers(); | ||
}); | ||
afterAll(() => server.close()); | ||
|
||
it('should render the row', () => { | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<VeeamCapacityOverviewRow bucketName="testBucket" /> | ||
</QueryClientProvider>, | ||
{ wrapper: NewWrapper() }, | ||
); | ||
expect(screen.getByText('Max repository Capacity')).toBeInTheDocument(); | ||
expect(screen.getByText('100 B')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not render the row if SOSAPI is not enabled', () => { | ||
mockUseBucketTagging.mockReturnValue({ | ||
status: 'success', | ||
value: { BUCKET_TAG_VEEAM_APPLICATION: 'OTHER_APPLICATION' }, | ||
}); | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<VeeamCapacityOverviewRow bucketName="testBucket" /> | ||
</QueryClientProvider>, | ||
); | ||
expect( | ||
screen.queryByText('Max repository Capacity'), | ||
).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should display loading state', () => { | ||
mockUseQuery.mockReturnValue({ | ||
data: null, | ||
status: 'loading', | ||
}); | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<VeeamCapacityOverviewRow bucketName="testBucket" /> | ||
</QueryClientProvider>, | ||
); | ||
expect(screen.getByText('Loading...')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should display error state', () => { | ||
mockUseQuery.mockReturnValue({ | ||
data: null, | ||
status: 'error', | ||
}); | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<VeeamCapacityOverviewRow bucketName="testBucket" /> | ||
</QueryClientProvider>, | ||
); | ||
expect(screen.getByText('Error')).toBeInTheDocument(); | ||
}); | ||
}); |
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,73 @@ | ||
import prettyBytes from 'pretty-bytes'; | ||
import { VEEAM_FEATURE } from '../../../js/config'; | ||
import { useBucketTagging } from '../../next-architecture/domain/business/buckets'; | ||
import { useConfig } from '../../next-architecture/ui/ConfigProvider'; | ||
import { useS3Client } from '../../next-architecture/ui/S3ClientProvider'; | ||
import { getVeeamObject } from '../../queries'; | ||
import * as T from '../TableKeyValue2'; | ||
import { VeeamCapacityModal } from './VeeamCapacityModal'; | ||
import { | ||
BUCKET_TAG_VEEAM_APPLICATION, | ||
VeeamApplicationType, | ||
} from './VeeamConstants'; | ||
import { decodeEntities } from './decodeEntities'; | ||
import { useQuery } from 'react-query'; | ||
|
||
export const VeeamCapacityOverviewRow = ({ | ||
bucketName, | ||
}: { | ||
bucketName: string; | ||
}) => { | ||
const s3Client = useS3Client(); | ||
const { tags } = useBucketTagging({ bucketName }); | ||
const { features } = useConfig(); | ||
const VEEAM_FEATURE_FLAG_ENABLED = features.includes(VEEAM_FEATURE); | ||
const veeamTagApplication = | ||
tags.status === 'success' && | ||
decodeEntities(tags.value?.[BUCKET_TAG_VEEAM_APPLICATION]); | ||
|
||
const isSOSAPIEnabled = | ||
veeamTagApplication === VeeamApplicationType.VEEAM_BACKUP_REPLICATION && | ||
VEEAM_FEATURE_FLAG_ENABLED; | ||
|
||
const { data: veeamObject, status: veeamObjectStatus } = useQuery( | ||
getVeeamObject({ | ||
bucketName, | ||
s3Client, | ||
}), | ||
); | ||
|
||
const xml = veeamObject?.Body?.toString(); | ||
const capacity = | ||
new DOMParser() | ||
?.parseFromString(xml || '', 'application/xml') | ||
?.querySelector('Capacity')?.textContent || '0'; | ||
const prettyBytesClusterCapacity = prettyBytes(parseInt(capacity, 10), { | ||
locale: 'en', | ||
binary: true, | ||
}); | ||
|
||
if (isSOSAPIEnabled) { | ||
return ( | ||
<T.Row> | ||
<T.Key> Max repository Capacity </T.Key> | ||
<T.GroupValues> | ||
<> | ||
{veeamObjectStatus === 'loading' | ||
? 'Loading...' | ||
: veeamObjectStatus === 'error' | ||
? 'Error' | ||
: prettyBytesClusterCapacity} | ||
</> | ||
<VeeamCapacityModal | ||
bucketName={bucketName} | ||
maxCapacity={prettyBytesClusterCapacity} | ||
status={veeamObjectStatus} | ||
/> | ||
</T.GroupValues> | ||
</T.Row> | ||
); | ||
} | ||
|
||
return <></>; | ||
}; |