Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Add unit test

Joshen Limf710b30b17b6
diff --git a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts
index 54b9745bfa5..71d74047ffe 100644
--- a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts
+++ b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts
@@ -2,18 +2,20 @@ import { StorageSizeUnits } from './StorageSettings.constants'
const k = 1024
-// [JOSHEN TODO] Write tests for this
export const convertFromBytes = (bytes: number) => {
// Up to GB since that's our storage upload limit
- if (bytes === 0) return { value: 0, unit: StorageSizeUnits.BYTES }
+ if (bytes <= 0) return { value: 0, unit: StorageSizeUnits.BYTES }
const i = Math.floor(Math.log(bytes) / Math.log(k))
- const unit = Object.values(StorageSizeUnits)[i]
- const value = bytes / Math.pow(k, i)
+ const formattedIdx = i > 3 ? 3 : i
+ const unit = Object.values(StorageSizeUnits)[formattedIdx]
+ const value = bytes / Math.pow(k, formattedIdx)
return { value, unit }
}
-export const convertToBytes = (size: number, unit: StorageSizeUnits) => {
+export const convertToBytes = (size: number, unit: StorageSizeUnits = StorageSizeUnits.BYTES) => {
const i = Object.values(StorageSizeUnits).indexOf(unit)
+ if (size < 0 || i < 0) return 0
+
return size * Math.pow(k, i)
}
diff --git a/studio/tests/components/Storage/StorageSettings.utils.test.js b/studio/tests/components/Storage/StorageSettings.utils.test.js
new file mode 100644
index 00000000000..6d5b960b04d
--- /dev/null
+++ b/studio/tests/components/Storage/StorageSettings.utils.test.js
@@ -0,0 +1,71 @@
+import { StorageSizeUnits } from 'components/to-be-cleaned/Storage/StorageSettings/StorageSettings.constants'
+import {
+ convertFromBytes,
+ convertToBytes,
+} from 'components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils'
+
+describe('StorageSettings.utils: convertFromBytes', () => {
+ test('should convert 1024 to 1KB', () => {
+ const mockInput = 1024
+ const output = convertFromBytes(mockInput)
+ expect(output).toStrictEqual({ value: 1, unit: StorageSizeUnits.KB })
+ })
+ test('should convert 5242880 to 50MB', () => {
+ const mockInput = 52428800
+ const output = convertFromBytes(mockInput)
+ expect(output).toStrictEqual({ value: 50, unit: StorageSizeUnits.MB })
+ })
+ test('should convert 100 to 100 bytes', () => {
+ const mockInput = 100
+ const output = convertFromBytes(mockInput)
+ expect(output).toStrictEqual({ value: 100, unit: StorageSizeUnits.BYTES })
+ })
+ test('should convert 5712306503.68 to 5.32GB', () => {
+ const mockInput = 5712306503.68
+ const output = convertFromBytes(mockInput)
+ expect(output).toStrictEqual({ value: 5.32, unit: StorageSizeUnits.GB })
+ })
+ test('should convert 9123162431 to 8.496607123874128GB', () => {
+ const mockInput = 9123162431
+ const output = convertFromBytes(mockInput)
+ expect(output).toStrictEqual({ value: 8.496607123874128, unit: StorageSizeUnits.GB })
+ })
+ test('should convert negative inputs to just 0', () => {
+ const mockInput = -1000
+ const output = convertFromBytes(mockInput)
+ expect(output).toStrictEqual({ value: 0, unit: StorageSizeUnits.BYTES })
+ })
+ test('should convert up to GB', () => {
+ const mockInput = 10737418240000
+ const output = convertFromBytes(mockInput)
+ expect(output).toStrictEqual({ value: 10000, unit: StorageSizeUnits.GB })
+ })
+})
+
+describe('StorageSettings.utils: convertToBytes', () => {
+ test('should be able to convert to bytes', () => {
+ const mockInput = 100
+ const output = convertToBytes(mockInput)
+ expect(output).toStrictEqual(100)
+ })
+ test('should be able to convert to KB', () => {
+ const output = convertToBytes(10, StorageSizeUnits.KB)
+ expect(output).toStrictEqual(10240)
+ })
+ test('should be able to convert to MB', () => {
+ const output = convertToBytes(51.2, StorageSizeUnits.MB)
+ expect(output).toStrictEqual(53687091.2)
+ })
+ test('should be able to convert to GB', () => {
+ const output = convertToBytes(10.21, StorageSizeUnits.GB)
+ expect(output).toStrictEqual(10962904023.04)
+ })
+ test('should be able to convert up to GB only', () => {
+ const output = convertToBytes(1.21, 'ZB')
+ expect(output).toStrictEqual(0)
+ })
+ test('should be able to handle negative inputs', () => {
+ const output = convertToBytes(-12312, StorageSizeUnits.KB)
+ expect(output).toStrictEqual(0)
+ })
+})