Skip to content

Commit

Permalink
refactor: Accept nulls for optional entity fields (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlovegrove authored Sep 13, 2022
1 parent 68060f2 commit 44cac53
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 256 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules/
.npm
.eslintcache
scratch.ts

.vscode/
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@doist/todoist-api-typescript",
"version": "2.0.0",
"version": "2.0.1",
"description": "A typescript wrapper for the Todoist REST API.",
"author": "Doist developers",
"repository": "[email protected]:doist/todoist-api-typescript.git",
Expand Down
42 changes: 15 additions & 27 deletions src/TodoistApi.comments.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { TodoistApi } from '.'
import {
COMMENT_WITH_ATTACHMENT_WITH_OPTIONALS_AS_NULL,
COMMENT_WITH_OPTIONALS_AS_NULL_PROJECT,
COMMENT_WITH_OPTIONALS_AS_NULL_TASK,
DEFAULT_AUTH_TOKEN,
DEFAULT_COMMENT,
DEFAULT_REQUEST_ID,
INVALID_ENTITY_ID,
} from './testUtils/testDefaults'
import { getRestBaseUri, ENDPOINT_REST_COMMENTS } from './consts/endpoints'
import { setupRestClientMock } from './testUtils/mocks'
import { assertInputValidationError } from './testUtils/asserts'

function getTarget() {
return new TodoistApi(DEFAULT_AUTH_TOKEN)
Expand All @@ -33,7 +34,12 @@ describe('TodoistApi comment endpoints', () => {
})

test('returns result from rest client', async () => {
const expectedComments = [DEFAULT_COMMENT]
const expectedComments = [
DEFAULT_COMMENT,
COMMENT_WITH_OPTIONALS_AS_NULL_TASK,
COMMENT_WITH_OPTIONALS_AS_NULL_PROJECT,
COMMENT_WITH_ATTACHMENT_WITH_OPTIONALS_AS_NULL,
]
setupRestClientMock(expectedComments)
const api = getTarget()

Expand All @@ -45,7 +51,7 @@ describe('TodoistApi comment endpoints', () => {

describe('getComment', () => {
test('calls get on expected url', async () => {
const commentId = 1
const commentId = '1'
const requestMock = setupRestClientMock(DEFAULT_COMMENT)
const api = getTarget()

Expand All @@ -65,16 +71,10 @@ describe('TodoistApi comment endpoints', () => {
setupRestClientMock(expectedComment)
const api = getTarget()

const comment = await api.getComment(1)
const comment = await api.getComment('1')

expect(comment).toEqual(expectedComment)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().getComment(INVALID_ENTITY_ID),
)
})
})

describe('addComment', () => {
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('TodoistApi comment endpoints', () => {
}

test('makes post request with expected params', async () => {
const taskId = 1
const taskId = '1'
const requestMock = setupRestClientMock(DEFAULT_COMMENT, 204)
const api = getTarget()

Expand All @@ -139,21 +139,15 @@ describe('TodoistApi comment endpoints', () => {
setupRestClientMock(returnedComment, 204)
const api = getTarget()

const result = await api.updateComment(1, updateCommentArgs)
const result = await api.updateComment('1', updateCommentArgs)

expect(result).toEqual(returnedComment)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().updateComment(INVALID_ENTITY_ID, updateCommentArgs),
)
})
})

describe('deleteComment', () => {
test('makes delete request on expected url', async () => {
const taskId = 1
const taskId = '1'
const requestMock = setupRestClientMock(undefined, 204)
const api = getTarget()

Expand All @@ -174,15 +168,9 @@ describe('TodoistApi comment endpoints', () => {
setupRestClientMock(undefined, 204)
const api = getTarget()

const result = await api.deleteComment(1)
const result = await api.deleteComment('1')

expect(result).toEqual(true)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().deleteComment(INVALID_ENTITY_ID),
)
})
})
})
39 changes: 7 additions & 32 deletions src/TodoistApi.labels.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { TodoistApi } from '.'
import {
DEFAULT_AUTH_TOKEN,
DEFAULT_LABEL,
DEFAULT_REQUEST_ID,
INVALID_ENTITY_ID,
} from './testUtils/testDefaults'
import { DEFAULT_AUTH_TOKEN, DEFAULT_LABEL, DEFAULT_REQUEST_ID } from './testUtils/testDefaults'
import { getRestBaseUri, ENDPOINT_REST_LABELS } from './consts/endpoints'
import { setupRestClientMock } from './testUtils/mocks'
import { assertInputValidationError } from './testUtils/asserts'

function getTarget() {
return new TodoistApi(DEFAULT_AUTH_TOKEN)
Expand All @@ -16,7 +10,7 @@ function getTarget() {
describe('TodoistApi label endpoints', () => {
describe('getLabel', () => {
test('calls get request with expected url', async () => {
const labelId = 12
const labelId = '12'
const requestMock = setupRestClientMock(DEFAULT_LABEL)
const api = getTarget()

Expand All @@ -35,16 +29,10 @@ describe('TodoistApi label endpoints', () => {
setupRestClientMock(DEFAULT_LABEL)
const api = getTarget()

const label = await api.getLabel(123)
const label = await api.getLabel('123')

expect(label).toEqual(DEFAULT_LABEL)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().getLabel(INVALID_ENTITY_ID),
)
})
})

describe('getLabels', () => {
Expand Down Expand Up @@ -112,7 +100,7 @@ describe('TodoistApi label endpoints', () => {
}

test('calls post on restClient with expected parameters', async () => {
const labelId = 123
const labelId = '123'
const requestMock = setupRestClientMock(DEFAULT_LABEL, 204)
const api = getTarget()

Expand All @@ -134,22 +122,15 @@ describe('TodoistApi label endpoints', () => {
setupRestClientMock(returnedTask, 204)
const api = getTarget()

const result = await api.updateLabel(123, DEFAULT_UPDATE_LABEL_ARGS)
const result = await api.updateLabel('123', DEFAULT_UPDATE_LABEL_ARGS)

expect(result).toEqual(returnedTask)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () =>
await getTarget().updateLabel(INVALID_ENTITY_ID, DEFAULT_UPDATE_LABEL_ARGS),
)
})
})

describe('deleteLabel', () => {
test('calls delete on expected label', async () => {
const labelId = 123
const labelId = '123'
const requestMock = setupRestClientMock(undefined, 204)
const api = getTarget()

Expand All @@ -170,15 +151,9 @@ describe('TodoistApi label endpoints', () => {
setupRestClientMock(undefined, 204)
const api = getTarget()

const result = await api.deleteLabel(123)
const result = await api.deleteLabel('123')

expect(result).toEqual(true)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().deleteLabel(INVALID_ENTITY_ID),
)
})
})
})
44 changes: 9 additions & 35 deletions src/TodoistApi.projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
DEFAULT_PROJECT,
DEFAULT_REQUEST_ID,
DEFAULT_USER,
INVALID_ENTITY_ID,
PROJECT_WITH_OPTIONALS_AS_NULL,
} from './testUtils/testDefaults'
import {
getRestBaseUri,
ENDPOINT_REST_PROJECTS,
ENDPOINT_REST_PROJECT_COLLABORATORS,
} from './consts/endpoints'
import { setupRestClientMock } from './testUtils/mocks'
import { assertInputValidationError } from './testUtils/asserts'

function getTarget() {
return new TodoistApi(DEFAULT_AUTH_TOKEN)
Expand All @@ -21,7 +20,7 @@ function getTarget() {
describe('TodoistApi project endpoints', () => {
describe('getProject', () => {
test('calls get request with expected url', async () => {
const projectId = 12
const projectId = '12'
const requestMock = setupRestClientMock(DEFAULT_PROJECT)
const api = getTarget()

Expand All @@ -40,16 +39,10 @@ describe('TodoistApi project endpoints', () => {
setupRestClientMock(DEFAULT_PROJECT)
const api = getTarget()

const project = await api.getProject(123)
const project = await api.getProject('123')

expect(project).toEqual(DEFAULT_PROJECT)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().getProject(INVALID_ENTITY_ID),
)
})
})

describe('getProjects', () => {
Expand All @@ -69,7 +62,7 @@ describe('TodoistApi project endpoints', () => {
})

test('returns result from rest client', async () => {
const projects = [DEFAULT_PROJECT]
const projects = [DEFAULT_PROJECT, PROJECT_WITH_OPTIONALS_AS_NULL]
setupRestClientMock(projects)
const api = getTarget()

Expand Down Expand Up @@ -114,7 +107,7 @@ describe('TodoistApi project endpoints', () => {
describe('updateProject', () => {
const DEFAULT_UPDATE_PROJECT_ARGS = { name: 'a name' }
test('calls post on restClient with expected parameters', async () => {
const projectId = 123
const projectId = '123'
const updateArgs = { name: 'a new name' }
const requestMock = setupRestClientMock(DEFAULT_PROJECT, 204)
const api = getTarget()
Expand All @@ -137,22 +130,15 @@ describe('TodoistApi project endpoints', () => {
setupRestClientMock(returnedProject, 204)
const api = getTarget()

const result = await api.updateProject(123, DEFAULT_UPDATE_PROJECT_ARGS)
const result = await api.updateProject('123', DEFAULT_UPDATE_PROJECT_ARGS)

expect(result).toEqual(returnedProject)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () =>
await getTarget().updateProject(INVALID_ENTITY_ID, DEFAULT_UPDATE_PROJECT_ARGS),
)
})
})

describe('deleteProject', () => {
test('calls delete on expected project', async () => {
const projectId = 123
const projectId = '123'
const requestMock = setupRestClientMock(undefined, 204)
const api = getTarget()

Expand All @@ -172,20 +158,14 @@ describe('TodoistApi project endpoints', () => {
setupRestClientMock(undefined, 204)
const api = getTarget()

const result = await api.deleteProject(123)
const result = await api.deleteProject('123')

expect(result).toEqual(true)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().deleteProject(INVALID_ENTITY_ID),
)
})
})

describe('getProjectCollaborators', () => {
const projectId = 123
const projectId = '123'
const users = [DEFAULT_USER]

test('calls get on expected endpoint', async () => {
Expand All @@ -211,11 +191,5 @@ describe('TodoistApi project endpoints', () => {

expect(returnedUsers).toEqual(users)
})

test('throws validation error for invalid id input', async () => {
await assertInputValidationError(
async () => await getTarget().getProjectCollaborators(INVALID_ENTITY_ID),
)
})
})
})
Loading

0 comments on commit 44cac53

Please sign in to comment.