Skip to content

Commit

Permalink
chore: Add test case for App recovers by DB query.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomenezes committed Dec 19, 2023
1 parent cb8f5b9 commit 3c39b34
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/handlers/OwnershipTransferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ export default class OwnershipTransferred implements Handler {

async handle(log: Log, _block: BlockData, ctx: DataHandlerContext<Store>) {
if (log.topics[0] === events.OwnershipTransferred.topic) {
const appId = log.transaction?.to?.toLowerCase();
const application = appId
? this.applicationStorage.get(appId) ??
(await ctx.store.get(Application, appId))
: undefined;
const appId = log.transaction?.to?.toLowerCase() ?? '';
const application =
this.applicationStorage.get(appId) ??
(await ctx.store.get(Application, appId));

if (application) {
// decode event
Expand Down
26 changes: 25 additions & 1 deletion tests/handlers/OwnershipTransferred.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';

import OwnerShipTransferred from '../../src/handlers/OwnershipTransferred';
import { Application } from '../../src/model';
import { block, ctx, logs } from '../stubs/params';

vi.mock('../../src/model/', async (importOriginal) => {
Expand All @@ -14,7 +15,7 @@ vi.mock('../../src/model/', async (importOriginal) => {

describe('ApplicationCreated', () => {
let ownershipTransferred: OwnerShipTransferred;
const mockApplicationStorage = new Map();
const mockApplicationStorage = new Map<String, Application>();
beforeEach(() => {
ownershipTransferred = new OwnerShipTransferred(mockApplicationStorage);
mockApplicationStorage.clear();
Expand All @@ -34,6 +35,7 @@ describe('ApplicationCreated', () => {
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;
Expand All @@ -50,5 +52,27 @@ describe('ApplicationCreated', () => {
'0xf05d57a5bed2d1b529c56001fc5810cc9afc0335',
);
});

test('should find application in the database and make the ownership transfer', async () => {
vi.spyOn(ctx.store, 'get').mockResolvedValueOnce({
id: logs[2].transaction.to,
owner: '0xf05d57a5bed2d1b529c56001fc5810cc9afc0335',
factory: {
id: '0x7122cd1221c20892234186facfe8615e6743ab02',
},
} as Application);

await ownershipTransferred.handle(logs[2], block, ctx);

expect(mockApplicationStorage.size).toBe(1);
const app = mockApplicationStorage.get(logs[2].transaction.to);

expect(app?.id).toEqual(
'0x7122cd1221c20892234186facfe8615e6743ab02',
);
expect(app?.owner).toEqual(
'0x96ae2ecbfde74b1ec55e9cf626ee80e4f64c8a63',
);
});
});
});

0 comments on commit 3c39b34

Please sign in to comment.