cards/pile.test.ts
Github Readme Stats Bot 63ea3ff952 Card and Pile tests
2026-01-17 18:10:52 -07:00

40 lines
1.4 KiB
TypeScript

import { expect, test, spyOn } from "bun:test"
import Pile from "./pile"
import Card, { Index, Suit } from "./card"
test('can assign Cards to a Pile', () => {
const fiveOfClubs = new Card(Index.Five, Suit.Club)
const aceOfSpades = new Card(Index.Ace, Suit.Spade)
const pile = new Pile([fiveOfClubs, aceOfSpades])
expect(pile.cards[0]).toBe(fiveOfClubs)
expect(pile.cards[1]).toBe(aceOfSpades)
})
test('can pretty print all Cards in a Pile', () => {
const fiveOfClubs = new Card(Index.Five, Suit.Club)
const aceOfSpades = new Card(Index.Ace, Suit.Spade)
const pile = new Pile([fiveOfClubs, aceOfSpades])
expect(pile.pretty).toBe(`Five of ♣\nAce of ♠`)
})
test('can shuffle a Pile', () => {
spyOn(global.Math, 'random').mockReturnValue(0.123);
const fiveOfClubs = new Card(Index.Five, Suit.Club)
const aceOfSpades = new Card(Index.Ace, Suit.Spade)
const queenOfHearts = new Card(Index.Queen, Suit.Heart)
const pile = new Pile([fiveOfClubs, aceOfSpades, queenOfHearts])
pile.shuffle()
expect(pile.cards.includes(fiveOfClubs)).toBe(true)
expect(pile.cards.includes(aceOfSpades)).toBe(true)
expect(pile.cards.includes(queenOfHearts)).toBe(true)
expect(pile.cards[0]).not.toBe(fiveOfClubs)
expect(pile.cards[1]).not.toBe(aceOfSpades)
expect(pile.cards[2]).not.toBe(queenOfHearts)
spyOn(global.Math, 'random').mockRestore();
}, { repeats: 9 })