Add examples for button factory
This commit is contained in:
29
examples/button-factory/package.json
Normal file
29
examples/button-factory/package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
10
examples/button-factory/src/button.js
Normal file
10
examples/button-factory/src/button.js
Normal 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;
|
||||||
|
}
|
||||||
16
examples/button-factory/src/button.test.js
Normal file
16
examples/button-factory/src/button.test.js
Normal 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 () => {});
|
||||||
|
});
|
||||||
9
examples/button-factory/src/local-storage.test.js
Normal file
9
examples/button-factory/src/local-storage.test.js
Normal 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);
|
||||||
|
});
|
||||||
29
examples/button-factory/src/secret-input.js
Normal file
29
examples/button-factory/src/secret-input.js
Normal 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;
|
||||||
|
}
|
||||||
6
examples/button-factory/src/secret-input.test.js
Normal file
6
examples/button-factory/src/secret-input.test.js
Normal 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 () => {});
|
||||||
7
examples/button-factory/vitest.config.ts
Normal file
7
examples/button-factory/vitest.config.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import { defineConfig } from 'vitest/config';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
environment: 'happy-dom',
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user