Remove tic tac toe

This commit is contained in:
Steve Kinney
2024-09-30 14:20:45 -06:00
parent 98b2aaa26d
commit 1158022cfb
6 changed files with 48 additions and 40 deletions

View File

@@ -1,3 +1,11 @@
function getStoredSecret() {
try {
return localStorage.getItem('secret') || '';
} catch {
return '';
}
}
export function createSecretInput() { export function createSecretInput() {
const id = 'secret-input'; const id = 'secret-input';
@@ -10,6 +18,7 @@ export function createSecretInput() {
input.type = 'password'; input.type = 'password';
input.name = 'secret'; input.name = 'secret';
input.placeholder = 'Enter your secret…'; input.placeholder = 'Enter your secret…';
input.value = getStoredSecret();
label.htmlFor = id; label.htmlFor = id;
label.textContent = 'Secret'; label.textContent = 'Secret';

View File

@@ -1,6 +1,36 @@
import { describe, expect, it, beforeEach } from 'vitest'; import { describe, expect, it, beforeEach, vi, afterEach } from 'vitest';
import { screen } from '@testing-library/dom'; import { screen } from '@testing-library/dom';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import { createSecretInput } from './secret-input.js'; import { createSecretInput } from './secret-input.js';
describe.todo('createSecretInput', async () => {}); import '@testing-library/jest-dom/vitest';
describe('createSecretInput', async () => {
beforeEach(() => {
vi.spyOn(localStorage, 'getItem').mockReturnValue('test secret');
vi.spyOn(localStorage, 'setItem');
document.body.innerHTML = '';
document.body.appendChild(createSecretInput());
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should have loaded the secret from localStorage', async () => {
expect(screen.getByLabelText('Secret')).toHaveValue('test secret');
expect(localStorage.getItem).toHaveBeenCalledWith('secret');
});
it('should save the secret to localStorage', async () => {
const input = screen.getByLabelText('Secret');
const button = screen.getByText('Store Secret');
await userEvent.clear(input);
await userEvent.type(input, 'new secret');
await userEvent.click(button);
expect(localStorage.setItem).toHaveBeenCalledWith('secret', 'new secret');
});
});

View File

@@ -1,25 +0,0 @@
{
"name": "tic-tac-toe",
"version": "1.0.0",
"description": "The classic game of Tic-Tac-Toe, but this time it's less fun because we're testing it.",
"main": "index.js",
"type": "module",
"scripts": {
"test": "vitest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/stevekinney/testing-javascript.git"
},
"author": "Steve Kinney <hello@stevekinney.net>",
"license": "MIT",
"bugs": {
"url": "https://github.com/stevekinney/testing-javascript/issues"
},
"homepage": "https://github.com/stevekinney/testing-javascript#readme",
"devDependencies": {
"@vitest/ui": "^2.1.1",
"vite": "^5.4.5",
"vitest": "^2.1.1"
}
}

View File

@@ -1,5 +0,0 @@
export default {
test: {
environment: 'node',
},
};

14
package-lock.json generated
View File

@@ -11,7 +11,6 @@
"workspaces": [ "workspaces": [
"packages/css-configuration", "packages/css-configuration",
"examples/calculator", "examples/calculator",
"examples/tic-tac-toe",
"examples/task-list", "examples/task-list",
"examples/authentication", "examples/authentication",
"examples/guess-the-number", "examples/guess-the-number",
@@ -131,8 +130,12 @@
}, },
"examples/logjam": { "examples/logjam": {
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "MIT",
"devDependencies": {} "devDependencies": {
"@vitest/ui": "^2.1.1",
"vite": "^5.4.5",
"vitest": "^2.1.1"
}
}, },
"examples/scratchpad": { "examples/scratchpad": {
"extraneous": true "extraneous": true
@@ -182,6 +185,7 @@
}, },
"examples/tic-tac-toe": { "examples/tic-tac-toe": {
"version": "1.0.0", "version": "1.0.0",
"extraneous": true,
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@vitest/ui": "^2.1.1", "@vitest/ui": "^2.1.1",
@@ -4754,10 +4758,6 @@
"node": ">=0.8" "node": ">=0.8"
} }
}, },
"node_modules/tic-tac-toe": {
"resolved": "examples/tic-tac-toe",
"link": true
},
"node_modules/tinybench": { "node_modules/tinybench": {
"version": "2.9.0", "version": "2.9.0",
"dev": true, "dev": true,

View File

@@ -28,7 +28,6 @@
"workspaces": [ "workspaces": [
"packages/css-configuration", "packages/css-configuration",
"examples/calculator", "examples/calculator",
"examples/tic-tac-toe",
"examples/task-list", "examples/task-list",
"examples/authentication", "examples/authentication",
"examples/guess-the-number", "examples/guess-the-number",