Skip to content

Commit

Permalink
test: [#44] some more tests for Queu class
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 17, 2022
1 parent 47f589d commit 73af831
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
45 changes: 45 additions & 0 deletions __tests__/unit/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../src/__tests__/helpers'

import {CommitAuthor} from '../../src/commit-author'
import {CommitInfo} from '../../src/commit-info'
import {CommitOptions} from '../../src/commit-options'
import {Queue} from '../../src/queue'
import {QueueName} from '../../src/queue-name'
Expand Down Expand Up @@ -49,6 +50,12 @@ async function createTestQueue(commitOptions: CommitOptions): Promise<Queue> {
}

describe('Queue', () => {
it('could be empty', async () => {
const queue = await createTestQueue(commitOptionsForTests())

expect(queue.isEmpty()).toBe(true)
})

it('should dispatch a new job', async () => {
const queue = await createTestQueue(commitOptionsForTests())

Expand All @@ -59,6 +66,20 @@ describe('Queue', () => {
expect(nextJob.payload()).toBe(dummyPayload())
})

it('should fail when trying to create a job if the previous job has not finished yet', async () => {
const queue = await createTestQueue(commitOptionsForTests())

const commit = await queue.createJob(dummyPayload())

const fn = async (): Promise<CommitInfo> => {
return queue.createJob(dummyPayload())
}

const expectedError = `Can't create a new job. There is already a pending job in commit: ${commit.hash}`

await expect(fn()).rejects.toThrowError(expectedError)
})

it('should mark a job as finished', async () => {
const queue = await createTestQueue(commitOptionsForTests())

Expand All @@ -70,6 +91,20 @@ describe('Queue', () => {
expect(nextJob.isNull()).toBe(true)
})

it('should fail when trying to finish a job without any pending to finish job', async () => {
const queue = await createTestQueue(commitOptionsForTests())

const fn = async (): Promise<CommitInfo> => {
return queue.markJobAsFinished(dummyPayload())
}

const expectedError = `Can't mark job as finished. There isn't any pending job in queue: ${queue
.getName()
.toString()}`

await expect(fn()).rejects.toThrowError(expectedError)
})

it('should allow to specify the commit author', async () => {
const queue = await createTestQueue(commitOptionsForTests())

Expand Down Expand Up @@ -133,4 +168,14 @@ describe('Queue', () => {

await expect(fn()).rejects.toThrowError(expectedError)
})

it('should return all the messages in the queue', async () => {
const queue = await createTestQueue(commitOptionsForTests())

await queue.createJob(dummyPayload())

const messages = queue.getMessages()

expect(messages).toContain(queue.getLatestMessage())
})
})
3 changes: 3 additions & 0 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export class Queue {
)
}

getName(): QueueName {
return this.name
}

getGitRepoDir(): GitRepoDir {
return this.gitRepo.getDir()
}
Expand Down

0 comments on commit 73af831

Please sign in to comment.