0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-31 22:51:25 -05:00

refactor(core): rename context to ctx in test file (#153)

This commit is contained in:
Xiao Yijun 2021-12-07 10:48:58 +08:00 committed by GitHub
parent f672f34b5c
commit b133b30b90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -37,74 +37,73 @@ afterEach(() => {
describe('request', () => {
it('should get limit and offset from queries', async () => {
const context = createContext({ page: '1', page_size: '30' });
await koaPagination()(context, next);
expect(context.pagination.limit).toEqual(30);
expect(context.pagination.offset).toEqual(0);
const ctx = createContext({ page: '1', page_size: '30' });
await koaPagination()(ctx, next);
expect(ctx.pagination.limit).toEqual(30);
expect(ctx.pagination.offset).toEqual(0);
});
it('should set default page to 1 (offset to 0) if non is provided', async () => {
const context = createContext({});
await koaPagination()(context, next);
expect(context.pagination.offset).toEqual(0);
const ctx = createContext({});
await koaPagination()(ctx, next);
expect(ctx.pagination.offset).toEqual(0);
});
it('should set default pageSize(limit) to 20', async () => {
const context = createContext({});
await koaPagination({ defaultPageSize: 20 })(context, next);
expect(context.pagination.limit).toEqual(20);
const ctx = createContext({});
await koaPagination({ defaultPageSize: 20 })(ctx, next);
expect(ctx.pagination.limit).toEqual(20);
});
it('throw when page value is not number-like', async () => {
const context = createContext({ page: 'invalid_number' });
await expect(koaPagination()(context, next)).rejects.toThrow();
const ctx = createContext({ page: 'invalid_number' });
await expect(koaPagination()(ctx, next)).rejects.toThrow();
});
it('throw when page number is 0', async () => {
const context = createContext({ page: '0' });
await expect(koaPagination()(context, next)).rejects.toThrow();
const ctx = createContext({ page: '0' });
await expect(koaPagination()(ctx, next)).rejects.toThrow();
});
it('throw when page_size value is not number-like', async () => {
const context = createContext({ page_size: 'invalid_number' });
await expect(koaPagination()(context, next)).rejects.toThrow();
const ctx = createContext({ page_size: 'invalid_number' });
await expect(koaPagination()(ctx, next)).rejects.toThrow();
});
it('throw when page_size number is 0', async () => {
const context = createContext({ page_size: '0' });
await expect(koaPagination()(context, next)).rejects.toThrow();
const ctx = createContext({ page_size: '0' });
await expect(koaPagination()(ctx, next)).rejects.toThrow();
});
it('throw when page_size number exceeds max', async () => {
const context = createContext({ page_size: '200' });
await expect(koaPagination({ maxPageSize: 100 })(context, next)).rejects.toThrow();
const ctx = createContext({ page_size: '200' });
await expect(koaPagination({ maxPageSize: 100 })(ctx, next)).rejects.toThrow();
});
});
/* eslint-disable @silverhand/fp/no-mutation */
describe('response', () => {
it('should add Total-Number to response header', async () => {
const context = createContext({});
await koaPagination()(context, async () => {
context.pagination.totalCount = 100;
const ctx = createContext({});
await koaPagination()(ctx, async () => {
ctx.pagination.totalCount = 100;
});
expect(setHeader).toHaveBeenCalledWith('Total-Number', '100');
});
describe('Link in response header', () => {
it('should append `first` and `last` in 1 of 1', async () => {
const context = createContext({});
await koaPagination({ defaultPageSize: 20 })(context, async () => {
context.pagination.totalCount = 10;
const ctx = createContext({});
await koaPagination({ defaultPageSize: 20 })(ctx, async () => {
ctx.pagination.totalCount = 10;
});
expect(links.has('<?page=1>; rel="first"')).toBeTruthy();
expect(links.has('<?page=1>; rel="last"')).toBeTruthy();
});
it('should append `first`, `next`, `last` in 1 of 2', async () => {
const context = createContext({});
await koaPagination({ defaultPageSize: 20 })(context, async () => {
context.pagination.totalCount = 30;
const ctx = createContext({});
await koaPagination({ defaultPageSize: 20 })(ctx, async () => {
ctx.pagination.totalCount = 30;
});
expect(links.has('<?page=1>; rel="first"')).toBeTruthy();
expect(links.has('<?page=2>; rel="next"')).toBeTruthy();
@ -112,9 +111,9 @@ describe('response', () => {
});
it('should append `first`, `prev`, `last` in 2 of 2', async () => {
const context = createContext({ page: '2' });
await koaPagination({ defaultPageSize: 20 })(context, async () => {
context.pagination.totalCount = 30;
const ctx = createContext({ page: '2' });
await koaPagination({ defaultPageSize: 20 })(ctx, async () => {
ctx.pagination.totalCount = 30;
});
expect(links.has('<?page=1>; rel="first"')).toBeTruthy();
expect(links.has('<?page=1>; rel="prev"')).toBeTruthy();
@ -122,9 +121,9 @@ describe('response', () => {
});
it('should append `first`, `prev`, `next`, `last` in 2 of 3', async () => {
const context = createContext({ page: '2' });
await koaPagination({ defaultPageSize: 20 })(context, async () => {
context.pagination.totalCount = 50;
const ctx = createContext({ page: '2' });
await koaPagination({ defaultPageSize: 20 })(ctx, async () => {
ctx.pagination.totalCount = 50;
});
expect(links.has('<?page=1>; rel="first"')).toBeTruthy();
expect(links.has('<?page=1>; rel="prev"')).toBeTruthy();
@ -133,4 +132,3 @@ describe('response', () => {
});
});
});
/* eslint-enable @silverhand/fp/no-mutation */