From 7b3597eb449d533d6b71f57eb9dcfcd7e671d169 Mon Sep 17 00:00:00 2001 From: einhorn_b Date: Mon, 10 Jul 2023 16:56:31 +0200 Subject: [PATCH] update tests --- dlt-connector/jest.config.js | 1 + .../resolver/TransactionsResolver.test.ts | 95 +++++++++++++++---- .../graphql/resolver/TransactionsResolver.ts | 4 +- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/dlt-connector/jest.config.js b/dlt-connector/jest.config.js index 24fc9fc68..aaf35eb74 100644 --- a/dlt-connector/jest.config.js +++ b/dlt-connector/jest.config.js @@ -18,6 +18,7 @@ module.exports = { '@input/(.*)': '/src/graphql/input/$1', '@proto/(.*)': '/src/proto/$1', '@test/(.*)': '/test/$1', + '@client/(.*)': '/src/client/$1', '@entity/(.*)': // eslint-disable-next-line n/no-process-env process.env.NODE_ENV === 'development' diff --git a/dlt-connector/src/graphql/resolver/TransactionsResolver.test.ts b/dlt-connector/src/graphql/resolver/TransactionsResolver.test.ts index dc668941b..75e390c3c 100644 --- a/dlt-connector/src/graphql/resolver/TransactionsResolver.test.ts +++ b/dlt-connector/src/graphql/resolver/TransactionsResolver.test.ts @@ -2,26 +2,11 @@ import 'reflect-metadata' import { createApolloTestServer } from '@test/ApolloServerMock' import assert from 'assert' -jest.mock('@iota/client', () => { - const mockMessageSender = jest.fn().mockImplementation(() => { - return { - index: jest.fn().mockReturnThis(), - data: jest.fn().mockReturnThis(), - submit: jest.fn().mockReturnValue({ - messageId: '5498130bc3918e1a7143969ce05805502417e3e1bd596d3c44d6a0adeea22710', - }), - } - }) - - const mockClient = { - message: mockMessageSender, - } - const mockClientBuilder = { - node: jest.fn().mockReturnThis(), - build: jest.fn(() => mockClient), - } +jest.mock('@/client/IotaClient', () => { return { - ClientBuilder: jest.fn(() => mockClientBuilder), + sendMessage: jest.fn().mockReturnValue({ + messageId: '5498130bc3918e1a7143969ce05805502417e3e1bd596d3c44d6a0adeea22710', + }), } }) @@ -48,4 +33,76 @@ describe('Transaction Resolver Test', () => { '5498130bc3918e1a7143969ce05805502417e3e1bd596d3c44d6a0adeea22710', ) }) + + it('test mocked sendTransaction invalid transactionType ', async () => { + const apolloTestServer = await createApolloTestServer() + + const response = await apolloTestServer.executeOperation({ + query: 'mutation ($input: TransactionInput!) { sendTransaction(data: $input) }', + variables: { + input: { + type: 'INVALID', + amount: '10', + created: 1688992436, + }, + }, + }) + assert(response.body.kind === 'single') + expect(response.body.singleResult).toMatchObject({ + errors: [ + { + message: + 'Variable "$input" got invalid value "INVALID" at "input.type"; Value "INVALID" does not exist in "TransactionType" enum.', + }, + ], + }) + }) + + it('test mocked sendTransaction invalid amount ', async () => { + const apolloTestServer = await createApolloTestServer() + + const response = await apolloTestServer.executeOperation({ + query: 'mutation ($input: TransactionInput!) { sendTransaction(data: $input) }', + variables: { + input: { + type: 'SEND', + amount: 'no number', + created: 1688992436, + }, + }, + }) + assert(response.body.kind === 'single') + expect(response.body.singleResult).toMatchObject({ + errors: [ + { + message: + 'Variable "$input" got invalid value "no number" at "input.amount"; Expected type "Decimal". [DecimalError] Invalid argument: no number', + }, + ], + }) + }) + + it('test mocked sendTransaction invalid created date ', async () => { + const apolloTestServer = await createApolloTestServer() + + const response = await apolloTestServer.executeOperation({ + query: 'mutation ($input: TransactionInput!) { sendTransaction(data: $input) }', + variables: { + input: { + type: 'SEND', + amount: '10', + created: '2023-03-02T10:12:00', + }, + }, + }) + assert(response.body.kind === 'single') + expect(response.body.singleResult).toMatchObject({ + errors: [ + { + message: + 'Variable "$input" got invalid value "2023-03-02T10:12:00" at "input.created"; Float cannot represent non numeric value: "2023-03-02T10:12:00"', + }, + ], + }) + }) }) diff --git a/dlt-connector/src/graphql/resolver/TransactionsResolver.ts b/dlt-connector/src/graphql/resolver/TransactionsResolver.ts index 739404007..c5d298cf3 100755 --- a/dlt-connector/src/graphql/resolver/TransactionsResolver.ts +++ b/dlt-connector/src/graphql/resolver/TransactionsResolver.ts @@ -24,7 +24,7 @@ export class TransactionResolver { ): Promise { const message = TransactionBody.fromObject(transaction) const messageBuffer = TransactionBody.encode(message).finish() - const resultMessage = iotaSendMessage(messageBuffer) - return (await resultMessage).messageId + const resultMessage = await iotaSendMessage(messageBuffer) + return resultMessage.messageId } }