Skip to content

Commit

Permalink
Improve typescript usage in tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
galkin authored Dec 6, 2023
1 parent d613cfc commit 36be251
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 30 deletions.
25 changes: 9 additions & 16 deletions javascript/src/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ import { Arguments } from './types'
import { parseFunctionArguments } from './utils/schema'
import { RetoolRPCVersion } from './version'

// Expose the protected methods for testing
class TestRetoolRPC extends RetoolRPC {
public testRegisterAgent() {
return this.protectedRegisterAgent()
}

public testFetchQueryAndExecute() {
return this.protectedFetchQueryAndExecute()
}
}

describe('RetoolRPC', () => {
const CURRENT_DATE = new Date(2012, 12, 21)
const resourceId = uuidv4()
Expand All @@ -47,7 +36,7 @@ describe('RetoolRPC', () => {
vi.resetAllMocks()
})

const rpcAgent = new TestRetoolRPC({
const rpcAgent = new RetoolRPC({
apiToken: 'secret-api-token',
host,
resourceId,
Expand Down Expand Up @@ -106,15 +95,17 @@ describe('RetoolRPC', () => {
beforeEach(async () => {
nock(host).post('/api/v1/retoolrpc/registerAgent').reply(200, { versionHash })

await rpcAgent.testRegisterAgent()
// @ts-expect-error: private method in test
await rpcAgent.registerAgent()
})

test('should "continue" if popQuery has no query', async () => {
const nockScope = nock(host)
.post('/api/v1/retoolrpc/popQuery', JSON.stringify(popQueryRequestBody))
.reply(200, { query: null })

const result = await rpcAgent.testFetchQueryAndExecute()
// @ts-expect-error: private method in test
const result = await rpcAgent.fetchQueryAndExecute()

expect(nockScope.isDone()).toBe(true)
expect(result).toEqual('continue')
Expand Down Expand Up @@ -168,7 +159,8 @@ describe('RetoolRPC', () => {
)
.reply(200, { success: true })

const result = await rpcAgent.testFetchQueryAndExecute()
// @ts-expect-error: private method in test
const result = await rpcAgent.fetchQueryAndExecute()

expect(nockScope1.isDone()).toBe(true)
expect(nockScope2.isDone()).toBe(true)
Expand Down Expand Up @@ -220,7 +212,8 @@ describe('RetoolRPC', () => {
)
.reply(200, { success: true })

const result = await rpcAgent.testFetchQueryAndExecute()
// @ts-expect-error: private method in test
const result = await rpcAgent.fetchQueryAndExecute()

expect(nockScope1.isDone()).toBe(true)
expect(nockScope2.isDone()).toBe(true)
Expand Down
14 changes: 0 additions & 14 deletions javascript/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,4 @@ export class RetoolRPC {

return 'continue'
}

/**
* Export registerAgent as a protected method for testing purpose only. Lookup TestRetoolRPC for usage.
*/
protected async protectedRegisterAgent() {
return this.registerAgent()
}

/**
* Export fetchQueryAndExecute as a protected method for testing purpose only. Lookup TestRetoolRPC for usage.
*/
protected async protectedFetchQueryAndExecute() {
return this.fetchQueryAndExecute()
}
}

0 comments on commit 36be251

Please sign in to comment.