Skip to content

Commit

Permalink
PR feedback for unit test clarifications
Browse files Browse the repository at this point in the history
Also added functional test to verify pageAll works with non-paging endpoints
  • Loading branch information
jkaster committed Jun 30, 2021
1 parent d37062f commit ca6618a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
26 changes: 26 additions & 0 deletions packages/sdk-node/test/methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,32 @@ describe('LookerNodeSDK', () => {
},
testTimeout
)
test(
'all_dashboards pageAll returns non-paged results',
async () => {
const sdk = new LookerSDK(session)
// Use a small limit to test paging for a small number of dashboards
let count = 0
let actual: IDashboard[] = []
const aggregate = (page: IDashboard[]) => {
console.log(`Page ${++count} has ${page.length} items`)
actual = actual.concat(page)
return page
}
const paged = await pageAll(
sdk,
() => sdk.all_dashboards('id,title'),
aggregate
)
expect(paged.limit).toEqual(-1)
expect(paged.more()).toEqual(false)

const all = await sdk.ok(sdk.all_dashboards('id, title'))
expect(actual.length).toEqual(all.length)
expect(actual).toEqual(all)
},
testTimeout
)
})
})

Expand Down
48 changes: 29 additions & 19 deletions packages/sdk-rtl/src/paging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const nextUrl =
'http://localhost/api/4.0/alerts/search?fields=id&limit=3&offset=6'

const firstLink = `<${firstUrl}>; rel="first"`
// Verify extra whitespace characters still get parsed correctly
// The Looker API provides a header without extra whitespace but other Link headers
// may not be formatted that way
const lastLink = `< ${lastUrl} >; rel="\tlast (end of line!)\t"`
const prevLink = `<\t${prevUrl}\n>;\nrel="prev"`
const nextLink = `<${nextUrl}>; rel="next"`
Expand Down Expand Up @@ -81,25 +84,31 @@ const session = new MockSession('mocked', settings, transport)
const sdk = new APIMethods(session, '4.0')
const mockedRows = ['one', 'two', 'three', 'four', 'five']
const totalCount = 10
const mockedRawResponse: IRawResponse = {
ok: true,
body: JSON.stringify(mockedRows),
headers: { [LinkHeader]: allLinks, [TotalCountHeader]: ` ${totalCount}` },
statusCode: 200,
statusMessage: 'Mocking',
contentType: 'application/json',
url: 'https://mocked',
}

const mockRawResponse = (url: string, body?: any): IRawResponse => {
const result = { ...mockedRawResponse, ...{ url: url } }
const mockRawResponse = (url?: string, body?: any): IRawResponse => {
const result: IRawResponse = {
ok: true,
body: JSON.stringify(mockedRows),
headers: { [LinkHeader]: allLinks, [TotalCountHeader]: ` ${totalCount}` },
statusCode: 200,
statusMessage: 'Mocking',
contentType: 'application/json',
url: 'https://mocked',
}
if (url) {
result.url = url
}
if (body) {
result.body = body
}
return result
}

const mockRaw = (
async function mockSDKSuccess<T>(value: T) {
return Promise.resolve<SDKResponse<T, any>>({ ok: true, value })
}

const mockRawResponseSuccess = (
transport: BrowserTransport,
value: any,
rawResponse: IRawResponse
Expand All @@ -110,10 +119,6 @@ const mockRaw = (
return mockSDKSuccess(value)
}

async function mockSDKSuccess<T>(value: T) {
return Promise.resolve<SDKResponse<T, any>>({ ok: true, value })
}

// async function mockSDKError<T>(value: T) {
// return Promise.resolve<SDKResponse<any, T>>({ ok: false, error: value })
// }
Expand Down Expand Up @@ -158,7 +163,7 @@ describe('paging', () => {
beforeEach(() => {
jest
.spyOn(BrowserTransport.prototype, 'rawRequest')
.mockReturnValue(Promise.resolve(mockedRawResponse))
.mockReturnValue(Promise.resolve(mockRawResponse()))
})
afterAll(() => {
jest.clearAllMocks()
Expand All @@ -167,7 +172,12 @@ describe('paging', () => {
it('initializes', async () => {
const actual = await pager(
sdk,
() => mockRaw(transport, mockedRows, mockRawResponse(firstUrl)),
() =>
mockRawResponseSuccess(
transport,
mockedRows,
mockRawResponse(firstUrl)
),
{
timeout: 99,
}
Expand All @@ -189,7 +199,7 @@ describe('paging', () => {

it('supports paging', async () => {
const paged = await pager(sdk, () =>
mockRaw(transport, mockedRows, mockedRawResponse)
mockRawResponseSuccess(transport, mockedRows, mockRawResponse())
)
expect(paged).toBeDefined()
expect(paged.items).toEqual(mockedRows)
Expand Down

0 comments on commit ca6618a

Please sign in to comment.