@jeremyckahn/farmhand
Version:
A farming game
165 lines (144 loc) • 4.63 kB
text/typescript
import { vitest } from 'vitest'
import { standardCowColors, genders, cowColors } from '../enums.js'
import { generateCow } from './generateCow.js'
import { generateOffspringCow } from './generateOffspringCow.js'
import { chooseRandom } from './chooseRandom.js'
describe('generateOffspringCow', () => {
let maleCow: farmhand.cow, femaleCow: farmhand.cow
beforeEach(() => {
vitest.spyOn(Math, 'random').mockReturnValue(1)
maleCow = generateCow({
baseWeight: 2200,
color: standardCowColors.ORANGE,
colorsInBloodline: {
[]: true,
[]: true,
},
gender: genders.MALE,
})
femaleCow = generateCow({
baseWeight: 2000,
color: standardCowColors.GREEN,
colorsInBloodline: {
[]: true,
[]: true,
},
gender: genders.FEMALE,
})
})
test('generates offspring', () => {
expect(generateOffspringCow(maleCow, femaleCow, 'foo')).toMatchObject({
color: chooseRandom([femaleCow.color, maleCow.color]),
colorsInBloodline: {
[]: true,
[]: true,
[]: true,
[]: true,
},
baseWeight: 2100,
isBred: true,
ownerId: 'foo',
originalOwnerId: 'foo',
})
})
test('order of parents does not matter', () => {
const idProps = { id: '123' }
const { ...offspring1 } = generateOffspringCow(
maleCow,
femaleCow,
'foo',
idProps
)
const { ...offspring2 } = generateOffspringCow(
femaleCow,
maleCow,
'foo',
idProps
)
expect(offspring1).toEqual(offspring2)
})
test('two cows of the same gender throw an error', () => {
expect(() => generateOffspringCow(femaleCow, femaleCow, 'foo')).toThrow()
})
describe('rainbow cows', () => {
test('cows with all of the colors in their bloodline are rainbow cows', () => {
vitest.spyOn(Math, 'random').mockReturnValue(1)
maleCow = generateCow({
baseWeight: 2200,
color: standardCowColors.ORANGE,
colorsInBloodline: {
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
},
gender: genders.MALE,
})
femaleCow = generateCow({
baseWeight: 2000,
color: standardCowColors.GREEN,
colorsInBloodline: {
[]: true,
},
gender: genders.FEMALE,
})
expect(generateOffspringCow(maleCow, femaleCow, 'foo')).toMatchObject({
color: cowColors.RAINBOW,
colorsInBloodline: {
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
},
baseWeight: 2100,
isBred: true,
ownerId: 'foo',
originalOwnerId: 'foo',
})
})
test('rainbow color is not stored in bloodline', () => {
vitest.spyOn(Math, 'random').mockReturnValue(1)
maleCow = generateCow({
baseWeight: 2200,
color: cowColors.RAINBOW,
colorsInBloodline: {
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
},
gender: genders.FEMALE,
})
femaleCow = generateCow({
baseWeight: 2000,
color: standardCowColors.WHITE,
colorsInBloodline: {
[]: true,
},
gender: genders.MALE,
})
const { colorsInBloodline } = generateOffspringCow(
maleCow,
femaleCow,
'foo'
)
expect(colorsInBloodline).toEqual({
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
[]: true,
})
})
})
})