Skip to content

Commit

Permalink
chore: increased test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Sep 21, 2022
1 parent dc7334d commit 0b25ddc
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 113 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,6 @@ dist

# generated docs
docs

# IDE
.idea/
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testEnvironment: 'jsdom',
}
44 changes: 35 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"@types/jest": "^26.0.13",
"form-data": "^4.0.0",
"genversion": "^3.0.1",
"husky": "^4.3.0",
"jest": "^26.4.1",
Expand Down
47 changes: 5 additions & 42 deletions test/__snapshots__/storageApi.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Get all buckets 1`] = `
Array [
Object {
"created_at": "2021-02-17T04:43:32.770206+00:00",
"id": "bucket2",
"name": "bucket2",
"owner": "4d56e902-f0a0-4662-8448-a4d9e643c142",
"public": false,
"updated_at": "2021-02-17T04:43:32.770206+00:00",
},
Object {
"created_at": "2021-02-17T04:43:32.770206+00:00",
"id": "bucket3",
"name": "bucket3",
"owner": "4d56e902-f0a0-4662-8448-a4d9e643c142",
"public": false,
"updated_at": "2021-02-17T04:43:32.770206+00:00",
},
Object {
"created_at": "2021-02-25T09:23:01.58385+00:00",
"id": "bucket4",
"name": "bucket4",
"owner": "317eadce-631a-4429-a0bb-f19a7a517b4a",
"public": false,
"updated_at": "2021-02-25T09:23:01.58385+00:00",
},
Object {
"created_at": "2021-02-27T03:04:25.6386+00:00",
"id": "bucket5",
"name": "bucket5",
"owner": "317eadce-631a-4429-a0bb-f19a7a517b4a",
"public": false,
"updated_at": "2021-02-27T03:04:25.6386+00:00",
},
]
`;

exports[`Get bucket by id 1`] = `
exports[`bucket api Get bucket by id 1`] = `
Object {
"created_at": "2021-02-17T04:43:32.770206+00:00",
"id": "bucket2",
Expand All @@ -48,21 +11,21 @@ Object {
}
`;

exports[`Get bucket with wrong id 1`] = `[StorageApiError: The resource was not found]`;
exports[`bucket api Get bucket with wrong id 1`] = `[StorageApiError: The resource was not found]`;

exports[`delete bucket 1`] = `
exports[`bucket api delete bucket 1`] = `
Object {
"message": "Successfully deleted",
}
`;

exports[`empty bucket 1`] = `
exports[`bucket api empty bucket 1`] = `
Object {
"message": "Successfully emptied",
}
`;

exports[`update bucket 1`] = `
exports[`bucket api update bucket 1`] = `
Object {
"message": "Successfully updated",
}
Expand Down
31 changes: 27 additions & 4 deletions test/__snapshots__/storageFileApi.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`get public URL 1`] = `
exports[`bucket api Get bucket by id 1`] = `
Object {
"data": Object {
"publicUrl": "http://localhost:8000/storage/v1/object/public/my-new-public-bucket/profiles/myUniqueUserId/profile.png",
},
"created_at": "2021-02-17T04:43:32.770206+00:00",
"id": "bucket2",
"name": "bucket2",
"owner": "4d56e902-f0a0-4662-8448-a4d9e643c142",
"public": false,
"updated_at": "2021-02-17T04:43:32.770206+00:00",
}
`;

exports[`bucket api Get bucket with wrong id 1`] = `[StorageApiError: The resource was not found]`;

exports[`bucket api delete bucket 1`] = `
Object {
"message": "Successfully deleted",
}
`;

exports[`bucket api empty bucket 1`] = `
Object {
"message": "Successfully emptied",
}
`;

exports[`bucket api update bucket 1`] = `
Object {
"message": "Successfully updated",
}
`;
1 change: 1 addition & 0 deletions test/fixtures/upload/file-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
supabase txt file 2
1 change: 1 addition & 0 deletions test/fixtures/upload/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
supabase txt file
107 changes: 54 additions & 53 deletions test/storageApi.test.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
import StorageBucketApi from '../src/packages/StorageBucketApi'
import { StorageClient } from '../src/index'

// TODO: need to setup storage-api server for this test
const URL = 'http://localhost:8000/storage/v1'
const KEY =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTYwMzk2ODgzNCwiZXhwIjoyNTUwNjUzNjM0LCJhdWQiOiIiLCJzdWIiOiIzMTdlYWRjZS02MzFhLTQ0MjktYTBiYi1mMTlhN2E1MTdiNGEiLCJSb2xlIjoicG9zdGdyZXMifQ.pZobPtp6gDcX0UbzMmG3FHSlg4m4Q-22tKtGWalOrNo'

const storage = new StorageBucketApi(URL, { Authorization: `Bearer ${KEY}` })
const storage = new StorageClient(URL, { Authorization: `Bearer ${KEY}` })
const newBucketName = `my-new-bucket-${Date.now()}`

test('Build to succeed', async () => {
// Basic test to ensure TS build is working.
expect(true).toEqual(true)
})

test('Get all buckets', async () => {
const res = await storage.listBuckets()
expect(res.data).not.toBeNull()
expect(res.data).toMatchSnapshot()
})

test('Get bucket by id', async () => {
const res = await storage.getBucket('bucket2')
expect(res.data).toMatchSnapshot()
})

test('Get bucket with wrong id', async () => {
const res = await storage.getBucket('not-exist-id')
expect(res.error).toMatchSnapshot()
})

test('create new bucket', async () => {
const res = await storage.createBucket(newBucketName)
expect(res.data?.name).toEqual(newBucketName)
})

test('create new public bucket', async () => {
const newPublicBucketName = 'my-new-public-bucket'
await storage.createBucket(newPublicBucketName, { public: true })
const res = await storage.getBucket(newPublicBucketName)
expect(res.data!.public).toBe(true)
})

test('update bucket', async () => {
const updateRes = await storage.updateBucket(newBucketName, { public: true })
expect(updateRes.error).toBeNull()
expect(updateRes.data).toMatchSnapshot()
const getRes = await storage.getBucket(newBucketName)
expect(getRes.data!.public).toBe(true)
})

test('empty bucket', async () => {
const res = await storage.emptyBucket(newBucketName)
expect(res.error).toBeNull()
expect(res.data).toMatchSnapshot()
})

test('delete bucket', async () => {
const res = await storage.deleteBucket(newBucketName)
expect(res.error).toBeNull()
expect(res.data).toMatchSnapshot()
describe('bucket api', () => {
test('Build to succeed', async () => {
// Basic test to ensure TS build is working.
expect(true).toEqual(true)
})

test('Get all buckets', async () => {
const res = await storage.listBuckets()
expect(res.data).not.toBeNull()
})

test('Get bucket by id', async () => {
const res = await storage.getBucket('bucket2')
expect(res.data).toMatchSnapshot()
})

test('Get bucket with wrong id', async () => {
const res = await storage.getBucket('not-exist-id')
expect(res.error).toMatchSnapshot()
})

test('create new bucket', async () => {
const res = await storage.createBucket(newBucketName)
expect(res.data?.name).toEqual(newBucketName)
})

test('create new public bucket', async () => {
const newPublicBucketName = 'my-new-public-bucket'
await storage.createBucket(newPublicBucketName, { public: true })
const res = await storage.getBucket(newPublicBucketName)
expect(res.data!.public).toBe(true)
})

test('update bucket', async () => {
const updateRes = await storage.updateBucket(newBucketName, { public: true })
expect(updateRes.error).toBeNull()
expect(updateRes.data).toMatchSnapshot()
const getRes = await storage.getBucket(newBucketName)
expect(getRes.data!.public).toBe(true)
})

test('empty bucket', async () => {
const res = await storage.emptyBucket(newBucketName)
expect(res.error).toBeNull()
expect(res.data).toMatchSnapshot()
})

test('delete bucket', async () => {
const res = await storage.deleteBucket(newBucketName)
expect(res.error).toBeNull()
expect(res.data).toMatchSnapshot()
})
})
Loading

0 comments on commit 0b25ddc

Please sign in to comment.