Hello everyone!
I know FCC doesn’t cover testing with jest, but I’m hoping someone can help me out. I’ve already successfully exported a different factory function to a different jest test written in the same way, but this one doesn’t want to work. The problem function in question is the generateCoordinates method inside the gameboardFactory function. When I run the tests, I get the type error that gameboardFactory.generateCoordinates is not a function. I’m feeling like as ever it’s something simple I’m missing. Here are the two files:
gameboard.js
export let gameboardFactory = () => {
let coordinates = {
xsmall: null,
small: null,
medium: null
}
let checker = (arr, target) => {
return target.every(value => arr.includes(value));
}
let generateCoordinates = (size, length) => {
while (coordinates[size] == null) {
let array = [];
let flag = true;
let subArray = [];
for (let i = 0; i < 2; i++) {
subArray.push(Math.ceil(Math.random() * (11 - length)))
}
array.push(subArray);
for (let j = 0, value2 = (subArray[1] + 1); j < (length -1); j++, value2++) {
let subsub = [subArray[0]];
subsub.push(value2)
array.push(subsub)
}
for (let k = 0; k < array.length; k++) {
for (let l = 0; l < Object.values(coordinates).length; l++) { if (Object.values(coordinates)[l] == null) continue;
for (let m = 0; m < Object.values(coordinates)[l].length; m++) {
if (checker(Object.values(coordinates)[l][m], array[k])) {
flag = false;
}
}
}
}
if (flag == true) {coordinates[size] = array;}
}
}
return {
coordinates,
generateCoordinates
}
}
gameboard.test.js
const gameboard = require('../functions/gameboard');
const gameboardFactory = gameboard.gameboardFactory;
describe('gameboard tests', () => {
test('returns ship lengths', () => {
const testGame = gameboardFactory.generateCoordinates('small', 3)
expect(testGame.coordinates).toMatchObject({
xsmall: null,
small: [1,2],
medium: null
})
})
})
Also, I am aware that the way I have the test set up is that even if the function was not causing an error, that the test would not pass. I’m just trying to see first what is actually returning as the result.
P.S. if anyone has any advice on refactoring this code I’m open to that as well, it’s definitely a bit messy!
Thanks so much!