Add examples for button factory

This commit is contained in:
Steve Kinney
2024-09-30 10:31:43 -06:00
parent 2ca2a233f9
commit c03bb7e8cb
7 changed files with 106 additions and 0 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,16 @@
import { it, expect, describe } from 'vitest';
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,9 @@
import { it, expect } from 'vitest';
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,29 @@
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…';
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,6 @@
import { describe, expect, it, beforeEach } from 'vitest';
import { screen } from '@testing-library/dom';
import userEvent from '@testing-library/user-event';
import { createSecretInput } from './secret-input.js';
describe.todo('createSecretInput', async () => {});