Skip to content

Commit

Permalink
refactor nits on test
Browse files Browse the repository at this point in the history
  • Loading branch information
mssalemi committed Oct 3, 2024
1 parent 395b219 commit 7c60a20
Showing 1 changed file with 207 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,213 @@ describe('Logs', () => {
renderInstance.unmount()
})

test('renders NetworkAccessRequestExecutedLog correctly with success', async () => {
const webhookLogOutput = {
appLog: new NetworkAccessRequestExecutedLog({
attempt: 1,
connectTimeMs: 40,
writeReadTimeMs: 40,
httpRequest: NETWORK_ACCESS_HTTP_REQUEST,
httpResponse: NETWORK_ACCESS_HTTP_RESPONSE,
error: null,
}),
prefix: {
functionId: FUNCTION_ID,
logTimestamp: TIME,
description: 'network access request executed in 80 ms',
status: 'Success',
storeName: 'my-store',
source: SOURCE,
},
}

const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []})
vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs)

const renderInstance = render(
<Logs
pollOptions={{jwtToken: MOCKED_JWT_TOKEN, filters: EMPTY_FILTERS, cursor: MOCKED_CURSOR}}
resubscribeCallback={vi.fn().mockResolvedValueOnce(MOCKED_JWT_TOKEN)}
storeNameById={new Map()}
/>,
)

const lastFrame = renderInstance.lastFrame()

expect(unstyled(lastFrame!)).toMatchInlineSnapshot(`
"2024-06-18 16:02:04.868 my-store my-function Success network access request executed in 80 ms
Attempt: 1
Connect time: 40 ms
Write read time: 40 ms
HTTP request:
{
\\"url\\": \\"https://api.example.com/hello\\",
\\"method\\": \\"GET\\",
\\"headers\\": {},
\\"body\\": null,
\\"policy\\": {
\\"read_timeout_ms\\": 500
}
}
HTTP response:
{
\\"status\\": 200,
\\"body\\": \\"Success\\",
\\"headers\\": {
\\"header1\\": \\"value1\\"
}
}"
`)

renderInstance.unmount()
})

test('renders NetworkAccessRequestExecutedLog correctly with failure', async () => {
const webhookLogOutput = {
appLog: new NetworkAccessRequestExecutedLog({
attempt: 1,
connectTimeMs: null,
writeReadTimeMs: null,
httpRequest: NETWORK_ACCESS_HTTP_REQUEST,
httpResponse: null,
error: 'Timeout Error',
}),
prefix: {
functionId: FUNCTION_ID,
logTimestamp: TIME,
description: 'network access request executed',
storeName: 'my-store',
status: 'Failure',
source: SOURCE,
},
}

const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []})
vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs)

const renderInstance = render(
<Logs
pollOptions={{jwtToken: MOCKED_JWT_TOKEN, filters: EMPTY_FILTERS, cursor: MOCKED_CURSOR}}
resubscribeCallback={vi.fn().mockResolvedValueOnce(MOCKED_JWT_TOKEN)}
storeNameById={new Map()}
/>,
)

const lastFrame = renderInstance.lastFrame()

expect(unstyled(lastFrame!)).toMatchInlineSnapshot(`
"2024-06-18 16:02:04.868 my-store my-function Failure network access request executed
Attempt: 1
HTTP request:
{
\\"url\\": \\"https://api.example.com/hello\\",
\\"method\\": \\"GET\\",
\\"headers\\": {},
\\"body\\": null,
\\"policy\\": {
\\"read_timeout_ms\\": 500
}
}
Error: Timeout Error"
`)

renderInstance.unmount()
})

test('renders NetworkAccessRequestExecutionInBackgroundLog correctly with NoCachedResponse', async () => {
const webhookLogOutput = {
appLog: new NetworkAccessRequestExecutionInBackgroundLog({
reason: BackgroundExecutionReason.NoCachedResponse,
httpRequest: NETWORK_ACCESS_HTTP_REQUEST,
}),
prefix: {
functionId: FUNCTION_ID,
logTimestamp: TIME,
description: 'network access request executing in background',
storeName: 'my-store',
status: 'Success',
source: SOURCE,
},
}

const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []})
vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs)

const renderInstance = render(
<Logs
pollOptions={{jwtToken: MOCKED_JWT_TOKEN, filters: EMPTY_FILTERS, cursor: MOCKED_CURSOR}}
resubscribeCallback={vi.fn().mockResolvedValueOnce(MOCKED_JWT_TOKEN)}
storeNameById={new Map()}
/>,
)

const lastFrame = renderInstance.lastFrame()

expect(unstyled(lastFrame!)).toMatchInlineSnapshot(`
"2024-06-18 16:02:04.868 my-store my-function Success network access request executing in background
Reason: No cached response available
HTTP request:
{
\\"url\\": \\"https://api.example.com/hello\\",
\\"method\\": \\"GET\\",
\\"headers\\": {},
\\"body\\": null,
\\"policy\\": {
\\"read_timeout_ms\\": 500
}
}"
`)

renderInstance.unmount()
})

test('renders NetworkAccessRequestExecutionInBackgroundLog correctly with CacheAboutToExpire', async () => {
const webhookLogOutput = {
appLog: new NetworkAccessRequestExecutionInBackgroundLog({
reason: BackgroundExecutionReason.CacheAboutToExpire,
httpRequest: NETWORK_ACCESS_HTTP_REQUEST,
}),
prefix: {
functionId: FUNCTION_ID,
logTimestamp: TIME,
description: 'network access request executing in background',
storeName: 'my-store',
status: 'Success',
source: SOURCE,
},
}

const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []})
vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs)

const renderInstance = render(
<Logs
pollOptions={{jwtToken: MOCKED_JWT_TOKEN, filters: EMPTY_FILTERS, cursor: MOCKED_CURSOR}}
resubscribeCallback={vi.fn().mockResolvedValueOnce(MOCKED_JWT_TOKEN)}
storeNameById={new Map()}
/>,
)

const lastFrame = renderInstance.lastFrame()

expect(unstyled(lastFrame!)).toMatchInlineSnapshot(`
"2024-06-18 16:02:04.868 my-store my-function Success network access request executing in background
Reason: Cache is about to expire
HTTP request:
{
\\"url\\": \\"https://api.example.com/hello\\",
\\"method\\": \\"GET\\",
\\"headers\\": {},
\\"body\\": null,
\\"policy\\": {
\\"read_timeout_ms\\": 500
}
}"
`)

renderInstance.unmount()
})

// Add more tests for other webhook log types (NetworkAccessRequestExecutedLog, NetworkAccessRequestExecutionInBackgroundLog)
})

Expand Down

0 comments on commit 7c60a20

Please sign in to comment.