Skip to content

Commit

Permalink
Added more test and methods to userMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
kwelch committed Jan 4, 2019
1 parent ab65a28 commit f1e5574
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,29 @@ describe('request', () => {
})
);
});

it('combines request options properly', async() => {
const {users} = eventbrite({
token: MOCK_TOKEN,
baseUrl: MOCK_BASE_URL,
});
const email = '[email protected]';

await expect(users.emailLookup(email)).resolves.toEqual(
MOCK_TRANSFORMED_USERS_ME_RESPONSE_DATA
);

expect(getMockFetch()).toHaveBeenCalledTimes(1);
expect(getMockFetch()).toHaveBeenCalledWith(
`${MOCK_BASE_URL}/users/lookup/`,
expect.objectContaining({
method: 'POST',
body: JSON.stringify({email}),
headers: expect.objectContaining({
Authorization: `Bearer ${MOCK_TOKEN}`,
}),
})
);
});
});
});
67 changes: 67 additions & 0 deletions src/__tests__/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,70 @@ describe('users.me()', () => {
restoreMockFetch();
});
});

describe('users.get(id)', () => {
it('calls fetch and calls fetch with appropriate defaults', async() => {
mockFetch(getMockResponse(MOCK_USERS_ME_RESPONSE_DATA));

await expect(users.get('142429416488')).resolves.toEqual(
MOCK_TRANSFORMED_USERS_ME_RESPONSE_DATA
);

expect(getMockFetch()).toHaveBeenCalledTimes(1);
expect(getMockFetch()).toHaveBeenCalledWith(
'/users/142429416488/',
expect.objectContaining({})
);

restoreMockFetch();
});

it('should handle not found users', async() => {
mockFetch(
getMockResponse(
{
status_code: 404,
error_description: 'The user you requested does not exist.',
error: 'NOT_FOUND',
},
{status: 404}
)
);

await expect(users.get('123')).rejects.toMatchObject({
response: expect.objectContaining({
status: 404,
statusText: 'Not Found',
ok: false,
}),
parsedError: {
description: 'The user you requested does not exist.',
error: 'NOT_FOUND',
},
});

restoreMockFetch();
});
});

describe('users.emailLookup(email)', () => {
it('calls fetch and calls fetch with appropriate defaults', async() => {
mockFetch(getMockResponse(MOCK_USERS_ME_RESPONSE_DATA));
const email = '[email protected]';

await expect(users.emailLookup(email)).resolves.toEqual(
MOCK_TRANSFORMED_USERS_ME_RESPONSE_DATA
);

expect(getMockFetch()).toHaveBeenCalledTimes(1);
expect(getMockFetch()).toHaveBeenCalledWith(
'/users/lookup/',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({email}),
})
);

restoreMockFetch();
});
});
17 changes: 16 additions & 1 deletion src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export interface User {
}

export interface UserMethods {
[key: string]: () => Promise<{}>;
[key: string]: () => Promise<User>;
me: () => Promise<User>;
get: (id: string) => Promise<User>;
emailLookup: (email: string) => Promise<User>;
}

const SNAKE_CASE_MATCH = /_\w/g;
Expand Down Expand Up @@ -45,7 +47,20 @@ export default (request: JSONRequest): UserMethods => {
const me = () =>
request('/users/me/').then(transformKeysSnakeToCamel) as Promise<User>;

const get = (id: string) =>
request(`/users/${id}/`).then(transformKeysSnakeToCamel) as Promise<
User
>;

const emailLookup = (email: string) =>
request('/users/lookup/', {
method: 'POST',
body: JSON.stringify({email}),
}).then(transformKeysSnakeToCamel) as Promise<User>;

return {
me,
get,
emailLookup,
};
};

0 comments on commit f1e5574

Please sign in to comment.