diff --git a/package.json b/package.json index 1338843..c854b21 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "transform": { ".*": "/node_modules/babel-jest" }, - "testDirectoryName": "src/test", "moduleFileExtensions": [ "js" ] diff --git a/src/components/Cell.js b/src/components/Cell.js index c2cc4ec..93d4706 100644 --- a/src/components/Cell.js +++ b/src/components/Cell.js @@ -1,10 +1,27 @@ class Cell { - constructor() { - this.living = false; + constructor(living = false) { + this.living = living; + this.liveNeighbors = 0; } toggleLiving() { this.living = !this.living; } + addLiveNeighbor() { + this.liveNeighbors++; + } + setLiving() { + if (this.living && this.liveNeighbors !== 2 && this.liveNeighbors !== 3) { + return (this.living = false); + } + if (this.liveNeighbors === 3) { + return (this.living = true); + } + } } +// as a stream -> cellStream = Stream(Cell, () => Cell(cellStream.isLiving())) +// in this case GameField = { [x-y]: cellStream } +// communicating with neighbors = filter for (Boolean(Cell.living)) -> Cell neighbors.addLivingNeighbor +// controlling whether to call or not: filter for (Boolean(Cell.living) || Boolean(cell.liveNeighbors)) -> cellStream.next + module.exports = Cell; diff --git a/src/test/Cell.test.js b/src/test/Cell.test.js index 76aa04e..a1a5dce 100644 --- a/src/test/Cell.test.js +++ b/src/test/Cell.test.js @@ -2,25 +2,45 @@ const Cell = require("../components/Cell"); describe("Cell functionality", () => { test("dispatch toggleLiving state should mark living cell dead", () => { + const cell = new Cell(true); + cell.toggleLiving(); + expect(cell.living).toEqual(false); + }); + test("dispatch toggleLiving state should mark dead cell living", () => { const cell = new Cell(); cell.toggleLiving(); expect(cell.living).toEqual(true); }); - test.todo("dispatch toggleLiving state should mark dead cell living"); - test.todo( - "dispatch add Live Neighbor should increment live neighbors property" - ); - test.todo( - "dispatch setLiving should determine life based on neighbors (for living cells)" - ); - const livingFromLivingStates = new Array(8).map((_, i) => - i === 2 || i === 3 ? true : false - ); - test.todo( - "dispatch setLiving should determine life based on neighbors (for dead cells)" - ); - const livingFromDeadStates = new Array(8).map((_, i) => - i === 3 ? true : false - ); + test("dispatch add Live Neighbor should increment live neighbors property", () => { + const cell = new Cell(); + cell.addLiveNeighbor(); + expect(cell.liveNeighbors).toEqual(1); + }); + + const livingFromLivingStates = new Array(8) + .fill() + .map((_, i) => (i === 2 || i === 3 ? true : false)); + + livingFromLivingStates.forEach((state, liveNeighbors) => { + test(`dispatch setLiving on live cell with ${liveNeighbors} neighbors should result in living = ${state}`, () => { + const cell = new Cell(true); + new Array(liveNeighbors).fill().forEach((_) => cell.addLiveNeighbor()); + cell.setLiving(); + expect(cell.living).toEqual(state); + }); + }); + + const livingFromDeadStates = new Array(8) + .fill() + .map((_, i) => (i === 3 ? true : false)); + + livingFromDeadStates.forEach((state, liveNeighbors) => { + test(`dispatch setLiving on dead cell with ${liveNeighbors} neighbors should result in living = ${state}`, () => { + const cell = new Cell(); + new Array(liveNeighbors).fill().forEach((_) => cell.addLiveNeighbor()); + cell.setLiving(); + expect(cell.living).toEqual(state); + }); + }); test.todo("dispatch setLiving should reset live neighbors"); });