Rename helper library to get-error-message

This commit is contained in:
Steve Kinney
2024-10-01 16:58:41 -05:00
parent 417bcb39c9
commit 75e9bb7ca7
13 changed files with 34 additions and 20 deletions

View File

@@ -0,0 +1,10 @@
export function createButton() {
const button = document.createElement('button');
button.textContent = 'Click Me';
button.addEventListener('click', () => {
button.textContent = 'Clicked!';
});
return button;
}

View File

@@ -0,0 +1,15 @@
import { createButton } from './button.js';
describe('createButton', () => {
it('should create a button element', () => {
const button = createButton();
expect(button.tagName).toBe('BUTTON');
});
it('should have the text "Click Me"', () => {
const button = createButton();
expect(button.textContent).toBe('Click Me');
});
it.todo('should change the text to "Clicked!" when clicked', async () => {});
});

View File

@@ -0,0 +1,7 @@
it('should properly assign to localStorage', () => {
const key = 'secret';
const message = "It's a secret to everybody.";
localStorage.setItem(key, message);
expect(localStorage.getItem(key)).toBe(message);
});

View File

@@ -0,0 +1,38 @@
function getStoredSecret() {
try {
return localStorage.getItem('secret') || '';
} catch {
return '';
}
}
export function createSecretInput() {
const id = 'secret-input';
const container = document.createElement('div');
const input = document.createElement('input');
const label = document.createElement('label');
const button = document.createElement('button');
input.id = id;
input.type = 'password';
input.name = 'secret';
input.placeholder = 'Enter your secret…';
input.value = getStoredSecret();
label.htmlFor = id;
label.textContent = 'Secret';
button.id = `${id}-button`;
button.textContent = 'Store Secret';
button.addEventListener('click', () => {
localStorage.setItem('secret', input.value);
input.value = '';
});
container.appendChild(label);
container.appendChild(input);
container.appendChild(button);
return container;
}

View File

@@ -0,0 +1,35 @@
import { screen } from '@testing-library/dom';
import userEvent from '@testing-library/user-event';
import { createSecretInput } from './secret-input.js';
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');
});
});