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,29 @@
{
"name": "button-factory",
"version": "1.0.0",
"main": "src/index.js",
"type": "module",
"scripts": {
"start": "vitest --ui",
"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"
},
"dependencies": {
"lit": "^3.2.0",
"uuid": "^10.0.0"
}
}

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 () => {});

View File

@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'happy-dom',
},
});