mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-10 05:40:06 +00:00
chore: better test coverage
This commit is contained in:
24
src/utils/spec/parseSasViyaLog.spec.ts
Normal file
24
src/utils/spec/parseSasViyaLog.spec.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { parseSasViyaLog } from '../parseSasViyaLog'
|
||||
|
||||
describe('parseSasViyaLog', () => {
|
||||
it('should parse sas viya log if environment is Node', () => {
|
||||
const logResponse = {
|
||||
items: [{ line: 'Line 1' }, { line: 'Line 2' }, { line: 'Line 3' }]
|
||||
}
|
||||
|
||||
const expectedLog = 'Line 1\nLine 2\nLine 3'
|
||||
const result = parseSasViyaLog(logResponse)
|
||||
expect(result).toEqual(expectedLog)
|
||||
})
|
||||
|
||||
it('should handle exceptions and return the original logResponse', () => {
|
||||
// Create a logResponse that will cause an error in the mapping process.
|
||||
const logResponse: any = {
|
||||
items: null
|
||||
}
|
||||
// Since logResponse.items is null, the ternary operator returns the else branch.
|
||||
const expectedLog = JSON.stringify(logResponse)
|
||||
const result = parseSasViyaLog(logResponse)
|
||||
expect(result).toEqual(expectedLog)
|
||||
})
|
||||
})
|
||||
72
src/utils/spec/parseViyaDebugResponse.spec.ts
Normal file
72
src/utils/spec/parseViyaDebugResponse.spec.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { RequestClient } from '../../request/RequestClient'
|
||||
import { parseSasViyaDebugResponse } from '../parseViyaDebugResponse'
|
||||
|
||||
describe('parseSasViyaDebugResponse', () => {
|
||||
let requestClient: RequestClient
|
||||
const serverUrl = 'http://test-server.com'
|
||||
|
||||
beforeEach(() => {
|
||||
requestClient = {
|
||||
get: jest.fn()
|
||||
} as unknown as RequestClient
|
||||
})
|
||||
|
||||
it('should extract URL and call get for Viya 3.5 iframe style', async () => {
|
||||
const iframeUrl = '/path/to/log.json'
|
||||
const response = `<html><body><iframe style="width: 99%; height: 500px" src="${iframeUrl}"></iframe></body></html>`
|
||||
const resultData = { message: 'success' }
|
||||
|
||||
// Mock the get method to resolve with an object containing the JSON result as string.
|
||||
;(requestClient.get as jest.Mock).mockResolvedValue({
|
||||
result: JSON.stringify(resultData)
|
||||
})
|
||||
|
||||
const result = await parseSasViyaDebugResponse(
|
||||
response,
|
||||
requestClient,
|
||||
serverUrl
|
||||
)
|
||||
|
||||
expect(requestClient.get).toHaveBeenCalledWith(
|
||||
serverUrl + iframeUrl,
|
||||
undefined,
|
||||
'text/plain'
|
||||
)
|
||||
expect(result).toEqual(resultData)
|
||||
})
|
||||
|
||||
it('should extract URL and call get for Viya 4 iframe style', async () => {
|
||||
const iframeUrl = '/another/path/to/log.json'
|
||||
// Note: For Viya 4, the regex splits in such a way that the extracted URL includes an extra starting double-quote.
|
||||
// For example, the URL becomes: '"/another/path/to/log.json'
|
||||
const response = `<html><body><iframe style="width: 99%; height: 500px; background-color:Canvas;" src="${iframeUrl}"></iframe></body></html>`
|
||||
const resultData = { status: 'ok' }
|
||||
|
||||
;(requestClient.get as jest.Mock).mockResolvedValue({
|
||||
result: JSON.stringify(resultData)
|
||||
})
|
||||
|
||||
const result = await parseSasViyaDebugResponse(
|
||||
response,
|
||||
requestClient,
|
||||
serverUrl
|
||||
)
|
||||
// Expect the extra starting double-quote as per the current implementation.
|
||||
const expectedUrl = serverUrl + `"` + iframeUrl
|
||||
|
||||
expect(requestClient.get).toHaveBeenCalledWith(
|
||||
expectedUrl,
|
||||
undefined,
|
||||
'text/plain'
|
||||
)
|
||||
expect(result).toEqual(resultData)
|
||||
})
|
||||
|
||||
it('should throw an error if iframe URL is not found', async () => {
|
||||
const response = `<html><body>No iframe here</body></html>`
|
||||
|
||||
await expect(
|
||||
parseSasViyaDebugResponse(response, requestClient, serverUrl)
|
||||
).rejects.toThrow('Unable to find webout file URL.')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user