diff --git a/README.md b/README.md index 54d0909..207004a 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ If you would like to propose a challenge, this project is open source, so feel f modal Testing harness Testing Create harness +Test a simple checkbox
nx @@ -90,7 +91,7 @@ If you would like to propose a challenge, this project is open source, so feel f - +
Thomas Laforge
Thomas Laforge

27 🧩
Thomas Laforge
Thomas Laforge

28 🧩
diff --git a/apps/testing-checkbox/.eslintrc.json b/apps/testing-checkbox/.eslintrc.json new file mode 100644 index 0000000..b428c22 --- /dev/null +++ b/apps/testing-checkbox/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts"], + "rules": { + "@angular-eslint/directive-selector": [ + "error", + { + "type": "attribute", + "prefix": "app", + "style": "camelCase" + } + ], + "@angular-eslint/component-selector": [ + "error", + { + "type": "element", + "prefix": "app", + "style": "kebab-case" + } + ] + }, + "extends": [ + "plugin:@nx/angular", + "plugin:@angular-eslint/template/process-inline-templates" + ] + }, + { + "files": ["*.html"], + "extends": ["plugin:@nx/angular-template"], + "rules": {} + } + ] +} diff --git a/apps/testing-checkbox/README.md b/apps/testing-checkbox/README.md new file mode 100644 index 0000000..86d5dce --- /dev/null +++ b/apps/testing-checkbox/README.md @@ -0,0 +1,32 @@ +

Test of a simple checkbox

+ +> Author: Thomas Laforge + +### Information + +This is the perfect example to get started with `Testing Library`. + +You will need to only check if the button gets enabled when clicking on the checkbox + +You can look into debug function to get full power of `Testing Library` like: + +- logRoles(...); +- screen.debug(); // you can pass a element as input +- screen.logTestingPlaygroundURL(); // you can pass a element as input + +### Submitting your work + +1. Fork the project +2. clone it +3. npm ci +4. `npx nx serve testing-checkbox` +5. _...work on it_ +6. Commit your work +7. Submit a PR with a title beginning with **Answer:28** that I will review and other dev can review. + +testing-checkbox +testing-checkbox solution author + + + +_You can ask any question on_ twitter diff --git a/apps/testing-checkbox/jest.config.ts b/apps/testing-checkbox/jest.config.ts new file mode 100644 index 0000000..461a89c --- /dev/null +++ b/apps/testing-checkbox/jest.config.ts @@ -0,0 +1,22 @@ +/* eslint-disable */ +export default { + displayName: 'testing-checkbox', + preset: '../../jest.preset.js', + setupFilesAfterEnv: ['/src/test-setup.ts'], + coverageDirectory: '../../coverage/apps/testing-checkbox', + transform: { + '^.+\\.(ts|mjs|js|html)$': [ + 'jest-preset-angular', + { + tsconfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.(html|svg)$', + }, + ], + }, + transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], + snapshotSerializers: [ + 'jest-preset-angular/build/serializers/no-ng-attributes', + 'jest-preset-angular/build/serializers/ng-snapshot', + 'jest-preset-angular/build/serializers/html-comment', + ], +}; diff --git a/apps/testing-checkbox/project.json b/apps/testing-checkbox/project.json new file mode 100644 index 0000000..6d3f734 --- /dev/null +++ b/apps/testing-checkbox/project.json @@ -0,0 +1,95 @@ +{ + "name": "testing-checkbox", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "projectType": "application", + "prefix": "app", + "sourceRoot": "apps/testing-checkbox/src", + "tags": [], + "targets": { + "build": { + "executor": "@angular-devkit/build-angular:browser", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/apps/testing-checkbox", + "index": "apps/testing-checkbox/src/index.html", + "main": "apps/testing-checkbox/src/main.ts", + "polyfills": ["zone.js"], + "tsConfig": "apps/testing-checkbox/tsconfig.app.json", + "assets": [ + "apps/testing-checkbox/src/favicon.ico", + "apps/testing-checkbox/src/assets" + ], + "styles": ["apps/testing-checkbox/src/styles.scss"], + "scripts": [] + }, + "configurations": { + "production": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "2kb", + "maximumError": "4kb" + } + ], + "outputHashing": "all" + }, + "development": { + "buildOptimizer": false, + "optimization": false, + "vendorChunk": true, + "extractLicenses": false, + "sourceMap": true, + "namedChunks": true + } + }, + "defaultConfiguration": "production" + }, + "serve": { + "executor": "@angular-devkit/build-angular:dev-server", + "configurations": { + "production": { + "browserTarget": "testing-checkbox:build:production" + }, + "development": { + "browserTarget": "testing-checkbox:build:development" + } + }, + "defaultConfiguration": "development" + }, + "extract-i18n": { + "executor": "@angular-devkit/build-angular:extract-i18n", + "options": { + "browserTarget": "testing-checkbox:build" + } + }, + "lint": { + "executor": "@nx/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": [ + "apps/testing-checkbox/**/*.ts", + "apps/testing-checkbox/**/*.html" + ] + } + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "apps/testing-checkbox/jest.config.ts", + "passWithNoTests": true + }, + "configurations": { + "ci": { + "ci": true, + "codeCoverage": true + } + } + } + } +} diff --git a/apps/testing-checkbox/src/app/app.component.spec.ts b/apps/testing-checkbox/src/app/app.component.spec.ts new file mode 100644 index 0000000..bb64b92 --- /dev/null +++ b/apps/testing-checkbox/src/app/app.component.spec.ts @@ -0,0 +1,10 @@ +import { render } from '@testing-library/angular'; +import { AppComponent } from './app.component'; + +describe('AppComponent', () => { + describe('When checking the checkbox', () => { + it('Then button is enabled', async () => { + await render(AppComponent); + }); + }); +}); diff --git a/apps/testing-checkbox/src/app/app.component.ts b/apps/testing-checkbox/src/app/app.component.ts new file mode 100644 index 0000000..3a2eb36 --- /dev/null +++ b/apps/testing-checkbox/src/app/app.component.ts @@ -0,0 +1,27 @@ +import { Component } from '@angular/core'; + +@Component({ + standalone: true, + selector: 'app-root', + template: ` + + `, +}) +export class AppComponent { + check = false; + + toggleCheck() { + this.check = !this.check; + } +} diff --git a/apps/testing-checkbox/src/assets/.gitkeep b/apps/testing-checkbox/src/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apps/testing-checkbox/src/favicon.ico b/apps/testing-checkbox/src/favicon.ico new file mode 100644 index 0000000..317ebcb Binary files /dev/null and b/apps/testing-checkbox/src/favicon.ico differ diff --git a/apps/testing-checkbox/src/index.html b/apps/testing-checkbox/src/index.html new file mode 100644 index 0000000..c4f1121 --- /dev/null +++ b/apps/testing-checkbox/src/index.html @@ -0,0 +1,13 @@ + + + + + testing-checkbox + + + + + + + + diff --git a/apps/testing-checkbox/src/main.ts b/apps/testing-checkbox/src/main.ts new file mode 100644 index 0000000..31c5da4 --- /dev/null +++ b/apps/testing-checkbox/src/main.ts @@ -0,0 +1,4 @@ +import { bootstrapApplication } from '@angular/platform-browser'; +import { AppComponent } from './app/app.component'; + +bootstrapApplication(AppComponent).catch((err) => console.error(err)); diff --git a/apps/testing-checkbox/src/styles.scss b/apps/testing-checkbox/src/styles.scss new file mode 100644 index 0000000..77e408a --- /dev/null +++ b/apps/testing-checkbox/src/styles.scss @@ -0,0 +1,5 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +/* You can add global styles to this file, and also import other style files */ diff --git a/apps/testing-checkbox/src/test-setup.ts b/apps/testing-checkbox/src/test-setup.ts new file mode 100644 index 0000000..15de72a --- /dev/null +++ b/apps/testing-checkbox/src/test-setup.ts @@ -0,0 +1,2 @@ +import '@testing-library/jest-dom'; +import 'jest-preset-angular/setup-jest'; diff --git a/apps/testing-checkbox/tailwind.config.js b/apps/testing-checkbox/tailwind.config.js new file mode 100644 index 0000000..38183db --- /dev/null +++ b/apps/testing-checkbox/tailwind.config.js @@ -0,0 +1,14 @@ +const { createGlobPatternsForDependencies } = require('@nx/angular/tailwind'); +const { join } = require('path'); + +/** @type {import('tailwindcss').Config} */ +module.exports = { + content: [ + join(__dirname, 'src/**/!(*.stories|*.spec).{ts,html}'), + ...createGlobPatternsForDependencies(__dirname), + ], + theme: { + extend: {}, + }, + plugins: [], +}; diff --git a/apps/testing-checkbox/tsconfig.app.json b/apps/testing-checkbox/tsconfig.app.json new file mode 100644 index 0000000..fff4a41 --- /dev/null +++ b/apps/testing-checkbox/tsconfig.app.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [] + }, + "files": ["src/main.ts"], + "include": ["src/**/*.d.ts"], + "exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"] +} diff --git a/apps/testing-checkbox/tsconfig.editor.json b/apps/testing-checkbox/tsconfig.editor.json new file mode 100644 index 0000000..8ae117d --- /dev/null +++ b/apps/testing-checkbox/tsconfig.editor.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["src/**/*.ts"], + "compilerOptions": { + "types": ["jest", "node"] + } +} diff --git a/apps/testing-checkbox/tsconfig.json b/apps/testing-checkbox/tsconfig.json new file mode 100644 index 0000000..e01cf19 --- /dev/null +++ b/apps/testing-checkbox/tsconfig.json @@ -0,0 +1,32 @@ +{ + "compilerOptions": { + "target": "es2022", + "useDefineForClassFields": false, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.spec.json" + }, + { + "path": "./tsconfig.editor.json" + } + ], + "extends": "../../tsconfig.base.json", + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/apps/testing-checkbox/tsconfig.spec.json b/apps/testing-checkbox/tsconfig.spec.json new file mode 100644 index 0000000..1a4817a --- /dev/null +++ b/apps/testing-checkbox/tsconfig.spec.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node", "@testing-library/jest-dom"] + }, + "files": ["src/test-setup.ts"], + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +}