From c6017db6f6dbe5f2d08447af63f5777efd279228 Mon Sep 17 00:00:00 2001 From: "dandhee.damarrama" Date: Tue, 3 Oct 2023 12:48:03 +0700 Subject: [PATCH] chore: add OwnerShipTransferred unit test --- tests/handlers/OwnershipTransferred.test.ts | 54 +++++++++++++++++++++ tests/stubs/params.ts | 38 +++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tests/handlers/OwnershipTransferred.test.ts diff --git a/tests/handlers/OwnershipTransferred.test.ts b/tests/handlers/OwnershipTransferred.test.ts new file mode 100644 index 0000000..dd978c6 --- /dev/null +++ b/tests/handlers/OwnershipTransferred.test.ts @@ -0,0 +1,54 @@ +import { beforeEach, describe, expect, test, vi } from 'vitest'; + +import OwnerShipTransferred from '../../src/handlers/OwnershipTransferred'; +import { block, ctx, logs } from '../stubs/params'; + +vi.mock('../../src/model/', async (importOriginal) => { + const actualMods = await importOriginal; + const Application = vi.fn(); + return { + ...actualMods!, + Application, + }; +}); + +describe('ApplicationCreated', () => { + let ownershipTransferred: OwnerShipTransferred; + const mockApplicationStorage = new Map(); + beforeEach(() => { + ownershipTransferred = new OwnerShipTransferred(mockApplicationStorage); + mockApplicationStorage.clear(); + vi.clearAllMocks(); + }); + describe('handle', async () => { + test('call with correct params', async () => { + vi.spyOn(ownershipTransferred, 'handle'); + ownershipTransferred.handle(logs[2], block, ctx); + expect(ownershipTransferred.handle).toHaveBeenCalledWith( + logs[2], + block, + ctx, + ); + }); + test('wrong contract address', async () => { + await ownershipTransferred.handle(logs[0], block, ctx); + expect(mockApplicationStorage.size).toBe(0); + }); + test('Ownership Transferred', async () => { + const mockApplicationStorage2 = new Map(); + const appId = logs[2].transaction.to; + mockApplicationStorage2.set(appId, { + id: appId, + owner: '0xf05d57a5bed2d1b529c56001fc5810cc9afc0335', + factory: { + id: '0x7122cd1221c20892234186facfe8615e6743ab02', + }, + }); + const ownerMock = new OwnerShipTransferred(mockApplicationStorage2); + await ownerMock.handle(logs[2], block, ctx); + expect(mockApplicationStorage2.get(appId).owner).not.toBe( + '0xf05d57a5bed2d1b529c56001fc5810cc9afc0335', + ); + }); + }); +}); diff --git a/tests/stubs/params.ts b/tests/stubs/params.ts index 2bb7788..b2fe834 100644 --- a/tests/stubs/params.ts +++ b/tests/stubs/params.ts @@ -71,6 +71,7 @@ export const logs = [ id: '0004411683-000001-cae3a', transactionIndex: 1, from: '0x74d093f6911ac080897c3145441103dabb869307', + to: '0x95ff8d3ce9dcb7455beb7845143bea84fe5c4f6f', hash: '0x1b165c2cd18cc58823fbe598e954458774a48f69249efed9ba5cf243b17d0d89', chainId: 11155111, value: '0', @@ -120,6 +121,43 @@ export const logs = [ }, }, }, + { + id: '0004412547-000071-ef8d2', + logIndex: 71, + transactionIndex: 38, + address: '0x5a1651482b751c1ecc36acb2fff5f31df24e7683', + topics: [ + '0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0', + '0x0000000000000000000000000000000000000000000000000000000000000000', + '0x00000000000000000000000096ae2ecbfde74b1ec55e9cf626ee80e4f64c8a63', + ], + data: '0x', + block: { + id: '0004412547-ef8d2', + height: 4412547, + hash: '0xef8d2ed24f024c5299be89a8cd518f69944c949f281f675f90aa360d108a2003', + parentHash: + '0xb73623fba8d50070a0f42eb884a8f28fc56a2af336d9721d3e25615d565b3f85', + timestamp: 1696292376000, + }, + transaction: { + id: '0004412547-000038-ef8d2', + transactionIndex: 38, + from: '0x96ae2ecbfde74b1ec55e9cf626ee80e4f64c8a63', + to: '0x7122cd1221c20892234186facfe8615e6743ab02', + hash: '0xc4df497a15a4afc64dcdb511be8d282283ddd99fc6b4760c74dbaf0570019aa0', + chainId: 11155111, + value: '0', + block: { + id: '0004412547-ef8d2', + height: 4412547, + hash: '0xef8d2ed24f024c5299be89a8cd518f69944c949f281f675f90aa360d108a2003', + parentHash: + '0xb73623fba8d50070a0f42eb884a8f28fc56a2af336d9721d3e25615d565b3f85', + timestamp: 1696292376000, + }, + }, + }, ]; export const block = { header: {