Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
shimkiv committed Oct 31, 2023
1 parent a1d77e4 commit 452a9b3
Show file tree
Hide file tree
Showing 6 changed files with 644 additions and 459 deletions.
40 changes: 9 additions & 31 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ function mockCoreMethods() {
jest.spyOn(core, 'setFailed').mockImplementation(jest.fn())
}

function mockHttpGet(response: any) {
jest.spyOn(HttpClient.prototype, 'get').mockResolvedValue(response)
}

function mockHttpPostJson(response: any) {
jest.spyOn(HttpClient.prototype, 'postJson').mockResolvedValue(response)
}
Expand All @@ -33,10 +29,6 @@ describe('Mina network GitHub Action', () => {
'max-attempts': '3',
'polling-interval-ms': '100'
})
mockHttpGet({
message: {} as any,
readBody: Promise.resolve({}) as any
})
mockHttpPostJson({
statusCode: 200,
headers: {},
Expand All @@ -48,11 +40,13 @@ describe('Mina network GitHub Action', () => {
})
mockCoreMethods()
await run()
expect(core.info).toHaveBeenCalledWith('Network is synced.')
expect(core.info).toHaveBeenCalledWith(
'\nBlockchain network is ready to use.'
)
})

it('should fail if GraphQL endpoint is never available', async () => {
mockHttpGet(Promise.reject(new Error('Network Error')))
mockHttpPostJson(Promise.reject(new Error('Network Error')))
mockCoreInputs({
'mina-graphql-port': '3085',
'max-attempts': '3',
Expand All @@ -61,15 +55,11 @@ describe('Mina network GitHub Action', () => {
mockCoreMethods()
await run()
expect(core.setFailed).toHaveBeenCalledWith(
'\nMaximum port check attempts reached. GraphQL port not available.'
'\nMaximum network sync attempts reached. The blockchain network is not ready!'
)
})

it('should fail if network does not sync within max attempts', async () => {
mockHttpGet({
message: {} as any,
readBody: Promise.resolve({}) as any
})
mockHttpPostJson({
statusCode: 200,
headers: {},
Expand All @@ -83,15 +73,11 @@ describe('Mina network GitHub Action', () => {
mockCoreMethods()
await run()
expect(core.setFailed).toHaveBeenCalledWith(
'\nMaximum network sync attempts reached. Network is not synced.'
'\nMaximum network sync attempts reached. The blockchain network is not ready!'
)
})

it('should retry when an empty GraphQL response is received', async () => {
mockHttpGet({
message: {} as any,
readBody: Promise.resolve({}) as any
})
mockHttpPostJson({
statusCode: 200,
headers: {},
Expand All @@ -105,15 +91,11 @@ describe('Mina network GitHub Action', () => {
mockCoreMethods()
await run()
expect(core.info).toHaveBeenCalledWith(
'Empty response received. Retrying in 0.1 seconds...'
'Blockchain network is not ready yet. Retrying in 0.1 seconds.'
)
})

it('should retry when GraphQL query returns an error', async () => {
mockHttpGet({
message: {} as any,
readBody: Promise.resolve({}) as any
})
mockHttpPostJson({
statusCode: 200,
headers: {},
Expand All @@ -127,15 +109,11 @@ describe('Mina network GitHub Action', () => {
mockCoreMethods()
await run()
expect(core.info).toHaveBeenCalledWith(
'Empty response received. Retrying in 0.1 seconds...'
'Blockchain network is not ready yet. Retrying in 0.1 seconds.'
)
})

it('should retry when a malformed GraphQL response is received', async () => {
mockHttpGet({
message: {} as any,
readBody: Promise.resolve({}) as any
})
mockHttpPostJson({
statusCode: 200,
headers: {},
Expand All @@ -149,7 +127,7 @@ describe('Mina network GitHub Action', () => {
mockCoreMethods()
await run()
expect(core.info).toHaveBeenCalledWith(
'Empty response received. Retrying in 0.1 seconds...'
'Blockchain network is not ready yet. Retrying in 0.1 seconds.'
)
})
})
Loading

0 comments on commit 452a9b3

Please sign in to comment.