-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(backend): write integration tests on folder
- Loading branch information
1 parent
3d327ca
commit d5797a8
Showing
5 changed files
with
275 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,7 +205,7 @@ describe('Test Authentication', () => { | |
password: 'password', | ||
}; | ||
|
||
const { authToken, userId } = await testHelper.createAuthenticatedUser({ ...input }); | ||
const { authToken, user } = await testHelper.createAuthenticatedUser({ ...input }); | ||
|
||
const authenticatedUserQuery = ` | ||
query AuthenticatedUser { | ||
|
@@ -241,7 +241,7 @@ describe('Test Authentication', () => { | |
expect(authenticatedUser).toMatchObject({ | ||
createdAt: expect.any(Number), | ||
email: input.email, | ||
id: userId, | ||
id: user.id, | ||
isEnabled: true, | ||
name: input.name, | ||
oauthProvider: 'email', | ||
|
@@ -259,7 +259,7 @@ describe('Test Authentication', () => { | |
}); | ||
|
||
test('Log out the authenticated user', async () => { | ||
const { authToken, userId } = await testHelper.createAuthenticatedUser({ | ||
const { authToken, user } = await testHelper.createAuthenticatedUser({ | ||
email: '[email protected]', | ||
name: 'Jane Doe', | ||
password: 'password', | ||
|
@@ -281,7 +281,7 @@ describe('Test Authentication', () => { | |
|
||
const { authenticatedUser } = response.body.data; | ||
|
||
expect(authenticatedUser.id).toEqual(userId); | ||
expect(authenticatedUser.id).toEqual(user.id); | ||
|
||
const logoutQuery = ` | ||
mutation LogoutUser { | ||
|
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
175 changes: 175 additions & 0 deletions
175
apps/backend/src/features/folders/graphql/folder.integration.spec.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,175 @@ | ||
import request from 'supertest'; | ||
|
||
import { TestHelper } from '../../../utils/tests/helpers'; | ||
import { TestServer, startTestServer } from '../../../utils/tests/server'; | ||
|
||
const graphqlEndpoint = '/graphql'; | ||
|
||
describe('Test Folder', () => { | ||
let server: TestServer; | ||
let testHelper: TestHelper; | ||
|
||
beforeAll(async () => { | ||
server = await startTestServer(); | ||
|
||
testHelper = new TestHelper(server.app, graphqlEndpoint); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testHelper.cleanDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await server.close(); | ||
}); | ||
|
||
test("Fail to create a folder when the parent folder doesn't exist", async () => { | ||
const { authToken } = await testHelper.createAuthenticatedUser({}); | ||
|
||
const query = ` | ||
mutation CreateFolder($input: CreateFolderInput!) { | ||
createFolder(input: $input) { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
const variables = { | ||
input: { | ||
name: 'My First Folder', | ||
parentId: 'non-existent-folder-id', | ||
}, | ||
}; | ||
|
||
const response = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query, variables }) | ||
.expect(200); | ||
|
||
const [error] = response.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('FOLDER_NOT_FOUND'); | ||
expect(error.message).toEqual('The folder with the id non-existent-folder-id not found'); | ||
}); | ||
|
||
test('Fail to create a folder when a folder with the same name already exists in the parent folder', async () => { | ||
const { authToken, user } = await testHelper.createAuthenticatedUser({}); | ||
|
||
await testHelper.createFolder(authToken, { | ||
name: 'My First Folder', | ||
parentId: user.rootFolderId, | ||
}); | ||
|
||
const query = ` | ||
mutation CreateFolder($input: CreateFolderInput!) { | ||
createFolder(input: $input) { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
const variables = { | ||
input: { | ||
name: 'My First Folder', | ||
parentId: user.rootFolderId, | ||
}, | ||
}; | ||
|
||
const response = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query, variables }) | ||
.expect(200); | ||
|
||
const [error] = response.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('FOLDER_ALREADY_EXIST'); | ||
expect(error.message).toEqual('A folder named "My First Folder" already exists'); | ||
}); | ||
|
||
// eslint-disable-next-line jest/no-disabled-tests | ||
test.skip("Fail to create a folder when the parent folder doesn't belong to the authenticated user", async () => { | ||
const { authToken } = await testHelper.createAuthenticatedUser({}); | ||
const { user: user2 } = await testHelper.createAuthenticatedUser({}); | ||
|
||
const query = ` | ||
mutation CreateFolder($input: CreateFolderInput!) { | ||
createFolder(input: $input) { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
const variables = { | ||
input: { | ||
name: 'My First Folder', | ||
parentId: user2.rootFolderId, | ||
}, | ||
}; | ||
|
||
const response = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query, variables }) | ||
.expect(200); | ||
|
||
const [error] = response.body.errors; | ||
|
||
expect(error.extensions.code).toEqual('FOLDER_NOT_BELONG_TO_USER'); | ||
expect(error.message).toEqual( | ||
`The folder with the id ${user2.rootFolderId} does not belong to the authenticated user`, | ||
); | ||
}); | ||
|
||
test('Successfully create a folder', async () => { | ||
const { authToken, user } = await testHelper.createAuthenticatedUser({}); | ||
|
||
const query = ` | ||
mutation CreateFolder($input: CreateFolderInput!) { | ||
createFolder(input: $input) { | ||
__typename | ||
id | ||
name | ||
isFavorite | ||
subFolders { | ||
id | ||
} | ||
subFoldersCount | ||
parent { | ||
id | ||
} | ||
user { | ||
id | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const variables = { | ||
input: { | ||
name: 'My First Folder', | ||
parentId: user.rootFolderId, | ||
}, | ||
}; | ||
|
||
const response = await request(server.app.getHttpServer()) | ||
.post(graphqlEndpoint) | ||
.set('Authorization', authToken) | ||
.send({ query, variables }) | ||
.expect(200); | ||
|
||
const { createFolder } = response.body.data; | ||
|
||
expect(createFolder).toMatchObject({ | ||
__typename: 'Folder', | ||
id: expect.any(String), | ||
isFavorite: false, | ||
name: 'My First Folder', | ||
parent: { id: user.rootFolderId }, | ||
subFolders: [], | ||
subFoldersCount: 0, | ||
user: { id: user.id }, | ||
}); | ||
}); | ||
}); |
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