Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for useEventLogs #7

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/lib/texas-holdem/useEventLogs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import useEventLogs, {EventLog} from "./useEventLogs";
import {renderHook, waitFor} from "@testing-library/react";
import EventEmitter from "eventemitter3";
import {TexasHoldemGameRoomEvents} from "./TexasHoldemGameRoom";

describe('useEventLogs', () => {
const testEventLog = <T extends EventLog['type'], E extends EventLog = EventLog & { type: T }>(
name: string,
emitEventFn: (listener: EventEmitter<TexasHoldemGameRoomEvents>) => void,
expectedEventType: T,
assertEventFn: (event: E) => void,
) => {
test(name, async () => {
const listener = new EventEmitter<TexasHoldemGameRoomEvents>();
const { result } = renderHook(() => useEventLogs(listener));

emitEventFn(listener);

await waitFor(() => {
expect(result.current.length).toBe(1);
});

const event = result.current[0];
expect(event.type).toBe(expectedEventType);
assertEventFn(event as E);
})
};

testEventLog('newRound', (listener) => {
listener.emit('players', 1, ['p1', 'p2']);
}, 'newRound', (event) => {
expect(event.players).toEqual(['p1', 'p2']);
expect(event.round).toBe(1);
});

testEventLog('bet (check)', (listener) => {
listener.emit('bet', 1, 0, 'p1', false);
}, 'check', (event) => {
expect(event.playerId).toBe('p1');
});

testEventLog('bet (raise)', (listener) => {
listener.emit('bet', 1, 1, 'p1', false);
}, 'raise', (event) => {
expect(event.raisedAmount).toBe(1);
expect(event.playerId).toBe('p1');
expect(event.allin).toBeFalsy();
});

testEventLog('bet all-in (raise)', (listener) => {
listener.emit('bet', 1, 2, 'p1', true);
}, 'raise', (event) => {
expect(event.raisedAmount).toBe(2);
expect(event.playerId).toBe('p1');
expect(event.allin).toBeTruthy();
});

testEventLog('fold', (listener) => {
listener.emit('fold', 1, 'p1');
}, 'fold', (event) => {
expect(event.playerId).toBe('p1');
});

testEventLog('winner', (listener) => {
listener.emit('winner', { how: 'LastOneWins', winner: 'p1', round: 1 });
}, 'winner', (event) => {
expect(event.result).toEqual({ how: 'LastOneWins', winner: 'p1', round: 1 });
});

testEventLog('fund', (listener) => {
listener.emit('fund', 1, 2, 'p1', false);
}, 'fund', (event) => {
expect(event.playerId).toBe('p1');
expect(event.currentAmount).toBe(1);
expect(event.previousAmount).toBe(2);
expect(event.borrowed).toBeFalsy();
});

testEventLog('fund (borrowed)', (listener) => {
listener.emit('fund', 1, 2, 'p1', true);
}, 'fund', (event) => {
expect(event.playerId).toBe('p1');
expect(event.currentAmount).toBe(1);
expect(event.previousAmount).toBe(2);
expect(event.borrowed).toBeTruthy();
});

testEventLog('fund (initial)', (listener) => {
listener.emit('fund', 100, undefined, 'p1', true);
}, 'fund', (event) => {
expect(event.playerId).toBe('p1');
expect(event.currentAmount).toBe(100);
expect(event.previousAmount).toBeUndefined();
expect(event.borrowed).toBeTruthy();
});
});
35 changes: 19 additions & 16 deletions src/lib/texas-holdem/useEventLogs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {useCallback, useEffect, useState} from "react";
import {TexasHoldem} from "../setup";
import {TexasHoldemGameRoomEvents, WinningResult} from "./TexasHoldemGameRoom";
import {EventListener} from "../types";

export interface NewRoundEventLog {
type: 'newRound';
Expand Down Expand Up @@ -55,7 +56,9 @@ export type EventLog =

export type EventLogs = EventLog[];

export default function useEventLogs(): EventLogs {
export default function useEventLogs(
listener: EventListener<TexasHoldemGameRoomEvents> = TexasHoldem.listener,
): EventLogs {
const [logs, setLogs] = useState<EventLog[]>([]);
const appendLog = useCallback((log: EventLog) => {
setLogs(prev => [...prev, log]);
Expand All @@ -70,11 +73,11 @@ export default function useEventLogs(): EventLogs {
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('players', playersListeners);
listener.on('players', playersListeners);
return () => {
TexasHoldem.listener.off('players', playersListeners);
listener.off('players', playersListeners);
};
}, [appendLog]);
}, [listener, appendLog]);

useEffect(() => {
const betListener: TexasHoldemGameRoomEvents['bet'] = (round, amount, who, allin) => {
Expand All @@ -94,11 +97,11 @@ export default function useEventLogs(): EventLogs {
});
}
};
TexasHoldem.listener.on('bet', betListener);
listener.on('bet', betListener);
return () => {
TexasHoldem.listener.off('bet', betListener);
listener.off('bet', betListener);
};
}, [appendLog]);
}, [listener, appendLog]);

useEffect(() => {
const foldListener: TexasHoldemGameRoomEvents['fold'] = (round, who) => {
Expand All @@ -108,11 +111,11 @@ export default function useEventLogs(): EventLogs {
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('fold', foldListener);
listener.on('fold', foldListener);
return () => {
TexasHoldem.listener.off('fold', foldListener);
listener.off('fold', foldListener);
};
}, [appendLog]);
}, [listener, appendLog]);

useEffect(() => {
const winnerListener: TexasHoldemGameRoomEvents['winner'] = (result) => {
Expand All @@ -122,11 +125,11 @@ export default function useEventLogs(): EventLogs {
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('winner', winnerListener);
listener.on('winner', winnerListener);
return () => {
TexasHoldem.listener.off('winner', winnerListener);
listener.off('winner', winnerListener);
};
}, [appendLog]);
}, [listener, appendLog]);

useEffect(() => {
const fundListener: TexasHoldemGameRoomEvents['fund'] = (currentAmount, previousAmount, playerId, borrowed) => {
Expand All @@ -139,11 +142,11 @@ export default function useEventLogs(): EventLogs {
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('fund', fundListener);
listener.on('fund', fundListener);
return () => {
TexasHoldem.listener.off('fund', fundListener);
listener.off('fund', fundListener);
};
}, [appendLog]);
}, [listener, appendLog]);

return logs;
}
Loading