Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Jul 31, 2024
1 parent 41bc538 commit 2773c0f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
81 changes: 81 additions & 0 deletions apps/web/src/actions/commits/fetchCommitsByProjectAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { CommitStatus, factories, Workspace } from '@latitude-data/core'
import useTestDatabase from '$core/tests/useTestDatabase'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { fetchCommitsByProjectAction } from './fetchCommitsByProjectAction'

const mocks = vi.hoisted(() => {
return {
getSession: vi.fn(),
}
})
vi.mock('$/services/auth/getSession', () => ({
getSession: mocks.getSession,
}))

let workspace: Workspace
describe('getUsersAction', () => {
useTestDatabase()

describe('unauthorized', () => {
it('errors when the user is not authenticated', async () => {
const [_, error] = await fetchCommitsByProjectAction({
status: CommitStatus.Draft,
projectId: 1,
})

expect(error!.name).toEqual('UnauthorizedError')
})
})

describe('authorized', () => {
beforeEach(async () => {
const { workspace: wp, userData } = await factories.createWorkspace({
name: 'test',
})
workspace = wp
mocks.getSession.mockReturnValue({
user: userData,
workspace: { id: workspace.id, name: workspace.name },
})
})

it('returns error when project is not found', async () => {
const [_, error] = await fetchCommitsByProjectAction({
status: CommitStatus.Draft,
projectId: 1,
})

expect(error!.name).toEqual('NotFoundError')
})

it('returns not found when project belongs to other workspce', async () => {
const { workspace: otherWorkspace } = await factories.createWorkspace({
name: 'test',
})
const { project } = await factories.createProject({
workspace: otherWorkspace,
})
const [_, error] = await fetchCommitsByProjectAction({
status: CommitStatus.Draft,
projectId: project.id,
})
expect(error!.name).toEqual('NotFoundError')
})

it('returns all draft commits', async () => {
const { project } = await factories.createProject({
workspace: workspace
})
await factories.createDraft({
project,
})
const [data] = await fetchCommitsByProjectAction({
status: CommitStatus.Draft,
projectId: project.id,
})

expect(data?.length).toEqual(1)
})
})
})
4 changes: 2 additions & 2 deletions apps/web/src/actions/commits/fetchCommitsByProjectAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { z } from 'zod'
import { withProject } from '../procedures'

// Not really using pagination yet
const RANDOM_ULTRA_LARGE_PAGE_SIZE = 1000
const ULTRA_LARGE_PAGE_SIZE = 1000

export const fetchCommitsByProjectAction = withProject
.createServerAction()
Expand All @@ -25,7 +25,7 @@ export const fetchCommitsByProjectAction = withProject
project: ctx.project,
filterByStatus: input.status,
page: input.page ?? 1,
pageSize: input.pageSize ?? RANDOM_ULTRA_LARGE_PAGE_SIZE,
pageSize: input.pageSize ?? ULTRA_LARGE_PAGE_SIZE,
})
.then((r) => r.unwrap())
.then((r) => r.map(commitPresenter))
Expand Down
File renamed without changes.

0 comments on commit 2773c0f

Please sign in to comment.