|
| 1 | +import { TodoistApi, type CurrentUser } from '.' |
| 2 | +import { DEFAULT_AUTH_TOKEN } from './testUtils/testDefaults' |
| 3 | +import { getSyncBaseUri, ENDPOINT_REST_USER } from './consts/endpoints' |
| 4 | +import { setupRestClientMock } from './testUtils/mocks' |
| 5 | + |
| 6 | +function getTarget(baseUrl = 'https://api.todoist.com') { |
| 7 | + return new TodoistApi(DEFAULT_AUTH_TOKEN, baseUrl) |
| 8 | +} |
| 9 | + |
| 10 | +const DEFAULT_CURRENT_USER_RESPONSE: CurrentUser = { |
| 11 | + id: '123456789', |
| 12 | + |
| 13 | + fullName: 'Test User', |
| 14 | + avatarBig: 'https://example.com/avatars/test_user_big.jpg', |
| 15 | + avatarMedium: 'https://example.com/avatars/test_user_medium.jpg', |
| 16 | + avatarS640: 'https://example.com/avatars/test_user_s640.jpg', |
| 17 | + avatarSmall: 'https://example.com/avatars/test_user_small.jpg', |
| 18 | + businessAccountId: null, |
| 19 | + isPremium: true, |
| 20 | + dateFormat: 0, |
| 21 | + timeFormat: 0, |
| 22 | + weeklyGoal: 100, |
| 23 | + dailyGoal: 10, |
| 24 | + completedCount: 102920, |
| 25 | + completedToday: 12, |
| 26 | + karma: 86394.0, |
| 27 | + karmaTrend: 'up', |
| 28 | + lang: 'en', |
| 29 | + nextWeek: 1, |
| 30 | + startDay: 1, |
| 31 | + startPage: 'project?id=test_project_123', |
| 32 | + tzInfo: { |
| 33 | + gmtString: '+02:00', |
| 34 | + hours: 2, |
| 35 | + isDst: 1, |
| 36 | + minutes: 0, |
| 37 | + timezone: 'Europe/Madrid', |
| 38 | + }, |
| 39 | + inboxProjectId: 'test_project_123', |
| 40 | + daysOff: [6, 7], |
| 41 | + weekendStartDay: 6, |
| 42 | +} |
| 43 | + |
| 44 | +describe('TodoistApi user endpoints', () => { |
| 45 | + describe('getUser', () => { |
| 46 | + test('calls get on restClient with expected parameters', async () => { |
| 47 | + const requestMock = setupRestClientMock(DEFAULT_CURRENT_USER_RESPONSE) |
| 48 | + const api = getTarget() |
| 49 | + |
| 50 | + await api.getUser() |
| 51 | + |
| 52 | + expect(requestMock).toHaveBeenCalledTimes(1) |
| 53 | + expect(requestMock).toHaveBeenCalledWith( |
| 54 | + 'GET', |
| 55 | + getSyncBaseUri(), |
| 56 | + ENDPOINT_REST_USER, |
| 57 | + DEFAULT_AUTH_TOKEN, |
| 58 | + ) |
| 59 | + }) |
| 60 | + |
| 61 | + test('calls get on restClient with expected parameters against staging', async () => { |
| 62 | + const requestMock = setupRestClientMock(DEFAULT_CURRENT_USER_RESPONSE) |
| 63 | + const stagingBaseUrl = 'https://api.todoist-staging.com' |
| 64 | + const api = getTarget(stagingBaseUrl) |
| 65 | + |
| 66 | + await api.getUser() |
| 67 | + |
| 68 | + expect(requestMock).toHaveBeenCalledTimes(1) |
| 69 | + expect(requestMock).toHaveBeenCalledWith( |
| 70 | + 'GET', |
| 71 | + getSyncBaseUri(stagingBaseUrl), |
| 72 | + ENDPOINT_REST_USER, |
| 73 | + DEFAULT_AUTH_TOKEN, |
| 74 | + ) |
| 75 | + }) |
| 76 | + |
| 77 | + test('handles user with null business account id', async () => { |
| 78 | + const responseWithNullBusinessAccount = { |
| 79 | + ...DEFAULT_CURRENT_USER_RESPONSE, |
| 80 | + businessAccountId: null, |
| 81 | + } |
| 82 | + setupRestClientMock(responseWithNullBusinessAccount) |
| 83 | + const api = getTarget() |
| 84 | + |
| 85 | + const actual = await api.getUser() |
| 86 | + |
| 87 | + expect(actual.businessAccountId).toBeNull() |
| 88 | + }) |
| 89 | + |
| 90 | + test('handles user with null avatar fields', async () => { |
| 91 | + const responseWithNullAvatars = { |
| 92 | + ...DEFAULT_CURRENT_USER_RESPONSE, |
| 93 | + avatarBig: null, |
| 94 | + avatarMedium: null, |
| 95 | + avatarS640: null, |
| 96 | + avatarSmall: null, |
| 97 | + } |
| 98 | + setupRestClientMock(responseWithNullAvatars) |
| 99 | + const api = getTarget() |
| 100 | + |
| 101 | + const actual = await api.getUser() |
| 102 | + |
| 103 | + expect(actual.avatarBig).toBeNull() |
| 104 | + expect(actual.avatarMedium).toBeNull() |
| 105 | + expect(actual.avatarS640).toBeNull() |
| 106 | + expect(actual.avatarSmall).toBeNull() |
| 107 | + }) |
| 108 | + |
| 109 | + test('handles user with tzInfo field', async () => { |
| 110 | + setupRestClientMock(DEFAULT_CURRENT_USER_RESPONSE) |
| 111 | + const api = getTarget() |
| 112 | + |
| 113 | + const actual = await api.getUser() |
| 114 | + |
| 115 | + expect(actual.tzInfo).toEqual({ |
| 116 | + gmtString: '+02:00', |
| 117 | + hours: 2, |
| 118 | + isDst: 1, |
| 119 | + minutes: 0, |
| 120 | + timezone: 'Europe/Madrid', |
| 121 | + }) |
| 122 | + expect(actual.tzInfo.timezone).toBe('Europe/Madrid') |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments