Skip to content

Commit

Permalink
split out some functions, add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skedwards88 committed Jul 21, 2024
1 parent 0964d5a commit f8da270
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 55 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/common/handleShare.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import sendAnalytics from "./sendAnalytics";

export function assembleShareLink({url, seed}) {
const fullUrl = seed ? `${url}?id=${seed}` : url;
export function assembleShareLink({url, seed, query="id"}) {
const fullUrl = seed ? `${url}?${query}=${seed}` : url;
return fullUrl;
}

export function handleShare({appName, text, url, seed}) {
const fullUrl = assembleShareLink({url, seed});
export function handleShare({appName, text, url, seed, query}) {
const fullUrl = assembleShareLink({url, seed, query});

navigator
.share({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {convertYYYYMMDDToDate} from "./convertYYYYMMDDToDate";

export function hasVisitedSince() {
export function hasVisitedSince(savedStateName, resetDateString) {
let lastVisitedYYYYMMDD = JSON.parse(
localStorage.getItem("blobbleLastVisited"),
localStorage.getItem(savedStateName),
);

if (!lastVisitedYYYYMMDD) {
Expand All @@ -11,10 +11,7 @@ export function hasVisitedSince() {

const lastVisitedDate = convertYYYYMMDDToDate(lastVisitedYYYYMMDD);

const resetDate = convertYYYYMMDDToDate("20240429");
const resetDate = convertYYYYMMDDToDate(resetDateString);

console.log(lastVisitedDate);
console.log(resetDate);
console.log(lastVisitedDate >= resetDate);
return lastVisitedDate >= resetDate;
}
26 changes: 26 additions & 0 deletions src/common/hasVisitedSince.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "jest-localstorage-mock";
import {hasVisitedSince} from "./hasVisitedSince";

describe("hasVisitedSince", () => {
const stateName = "testLastVisited";

test("returns false if lastVisited is not set", () => {
localStorage.removeItem(stateName);
expect(hasVisitedSince(stateName, "20240429")).toBe(false);
});

test("returns false if lastVisited is set to a date before the reset date", () => {
localStorage.setItem(stateName, JSON.stringify("20240428"));
expect(hasVisitedSince(stateName, "20240429")).toBe(false);
});

test("returns true if lastVisited is set to the reset date", () => {
localStorage.setItem(stateName, JSON.stringify("20240429"));
expect(hasVisitedSince(stateName, "20240429")).toBe(true);
});

test("returns true if lastVisited is set to a date after the reset date", () => {
localStorage.setItem(stateName, JSON.stringify("20240430"));
expect(hasVisitedSince(stateName, "20240429")).toBe(true);
});
});
32 changes: 12 additions & 20 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,18 @@ import {gameInit} from "../logic/gameInit";
import {gameReducer} from "../logic/gameReducer";
import getDailySeed from "../common/getDailySeed";
import {gameIsSolvedQ} from "../logic/gameIsSolvedQ";
import {getInitialState} from "../logic/getInitialState";
import {hasVisitedSince} from "../logic/hasVisitedSince";

function parseUrlQuery() {
const searchParams = new URLSearchParams(document.location.search);
const query = searchParams.get("id");

// The seed query consists of two parts: the seed and the difficulty level, separated by an underscore
let difficultyLevel;
let seed;
if (query) {
[seed, difficultyLevel] = query.split("_");
difficultyLevel = parseInt(difficultyLevel);
}

return [seed, difficultyLevel];
}
import {getInitialState} from "../common/getInitialState";
import {hasVisitedSince} from "../common/hasVisitedSince";
import {parseUrlQuery} from "../logic/parseUrlQuery";

export default function App() {
// If a query string was passed,
// parse it to get the data to regenerate the game described by the query string
const [seed, difficultyLevel] = parseUrlQuery();

const hasVisited = hasVisitedSince();
// Determine when the player last visited the game
// This is used to determine whether to show the rules or an announcement instead of the game
const hasVisited = hasVisitedSince("blobbleLastVisited", "20240429");
const [lastVisited] = React.useState(getDailySeed());
React.useEffect(() => {
window.localStorage.setItem(
Expand All @@ -42,13 +32,15 @@ export default function App() {
);
}, [lastVisited]);

// Determine what view to show the user
const savedDisplay = JSON.parse(
localStorage.getItem("blobbleDisplaySavedStateName"),
localStorage.getItem("blobbleDisplay"),
);
const [display, setDisplay] = React.useState(
getInitialState(savedDisplay, hasVisited),
);

// Set up states that will be used by the handleAppInstalled and handleBeforeInstallPrompt listeners
const [installPromptEvent, setInstallPromptEvent] = React.useState();
const [showInstallButton, setShowInstallButton] = React.useState(true);

Expand Down Expand Up @@ -116,7 +108,7 @@ export default function App() {

React.useEffect(() => {
window.localStorage.setItem(
"blobbleDisplaySavedStateName",
"blobbleDisplay",
JSON.stringify(display),
);
}, [display]);
Expand Down
2 changes: 1 addition & 1 deletion src/images/icons/heart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions src/logic/getConnectivity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {getSurroundingIndexes} from "@skedwards88/word_logic";

// Given indexes and a grid size
// Gets the number of neighboring (diagonals included) input indexes for each index
export function getConnectivity(indexes, gridSize) {
// error if grid size is 0 or negative
if (gridSize <= 0) {
throw new Error("Grid size must be greater than 0");
}

// error if index exceeds the grid size
if (indexes.some((index) => index >= gridSize * gridSize || index < 0)) {
throw new Error("Index is not within grid size");
}

let connectivity = 0;
indexes.forEach((index) => {
const surroundingIndexesInGrid = getSurroundingIndexes({
index,
numColumns: gridSize,
numRows: gridSize,
});
const surroundingIndexesInInput = surroundingIndexesInGrid.filter((neighbor) => neighbor != index && indexes.includes(neighbor));
connectivity += surroundingIndexesInInput.length;
});

return connectivity;
}
71 changes: 71 additions & 0 deletions src/logic/getConnectivity.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {getConnectivity} from "./getConnectivity";

describe("getConnectivity", () => {
test("returns the number of connections (directionally matters) between the input indexes (corner row)", () => {
const indexes = [0, 1, 2];
const gridSize = 4;
expect(getConnectivity(indexes, gridSize)).toBe(4);
});

test("returns the number of connections (directionally matters) between the input indexes (corner column)", () => {
const indexes = [8, 4, 0];
const gridSize = 4;
expect(getConnectivity(indexes, gridSize)).toBe(4);
});

test("returns the number of connections (directionally matters) between the input indexes (square)", () => {
const indexes = [5, 6, 10, 9];
const gridSize = 4;
expect(getConnectivity(indexes, gridSize)).toBe(12);
});

test("duplicate indexes are counted twice", () => {
const indexes = [5, 6, 10, 9, 5];
const gridSize = 4;
expect(getConnectivity(indexes, gridSize)).toBe(15);
});

test("works with singleton indexes input", () => {
const indexes = [5];
const gridSize = 4;
expect(getConnectivity(indexes, gridSize)).toBe(0);
});

test("works with empty indexes input", () => {
const indexes = [];
const gridSize = 4;
expect(getConnectivity(indexes, gridSize)).toBe(0);
});

test("throws an error when the indexes array has elements not in the grid", () => {
const indexes = [0, 4, 16];
const gridSize = 4;
expect(() => getConnectivity(indexes, gridSize)).toThrow(
"Index is not within grid size",
);
});

test("throws an error when the indexes array has negative elements", () => {
const indexes = [0, 4, -1];
const gridSize = 4;
expect(() => getConnectivity(indexes, gridSize)).toThrow(
"Index is not within grid size",
);
});

test("throws an error when the gridSize is 0", () => {
const indexes = [0, 1, 2];
const gridSize = 0;
expect(() => getConnectivity(indexes, gridSize)).toThrow(
"Grid size must be greater than 0",
);
});

test("throws an error when the gridSize is negative", () => {
const indexes = [0, 1, 2];
const gridSize = -4;
expect(() => getConnectivity(indexes, gridSize)).toThrow(
"Grid size must be greater than 0",
);
});
});
24 changes: 0 additions & 24 deletions src/logic/hasVisitedSince.test.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/logic/parseUrlQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function parseUrlQuery() {
const searchParams = new URLSearchParams(document.location.search);
const query = searchParams.get("id");

// The seed query consists of two parts: the seed and the difficulty level, separated by an underscore
let difficultyLevel;
let seed;
if (query) {
[seed, difficultyLevel] = query.split("_");
difficultyLevel = parseInt(difficultyLevel);
}

return [seed, difficultyLevel];
}

0 comments on commit f8da270

Please sign in to comment.