Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a "click counter button" in my vanilla TypeScript app
Read this commit's story on Medium: - https://link.medium.com/x3cuKM1rPdb
- Loading branch information
1 parent
21d7853
commit c0404bd
Showing
8 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { MountActivityFn } from '../activities.types'; | ||
import { clickToCountActivity } from './click-to-count'; | ||
|
||
export const mountClickToCount: MountActivityFn = function (mountPoint) { | ||
const clickToCount = clickToCountActivity(); | ||
|
||
mountPoint.appendChild(clickToCount); | ||
|
||
return mountPoint; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { clickToCountActivity } from './click-to-count'; | ||
|
||
describe('clickToCountActivity', () => { | ||
it('Should render a button', () => { | ||
const clickToCount = clickToCountActivity(); | ||
|
||
expect(clickToCount.nodeName).toEqual('BUTTON'); | ||
}); | ||
|
||
it('Should display a properly incrementing count', () => { | ||
const clickToCount = clickToCountActivity(); | ||
expect(clickToCount.textContent).toContain('0'); | ||
clickToCount.click(); | ||
expect(clickToCount.textContent).toContain('1'); | ||
clickToCount.click(); | ||
expect(clickToCount.textContent).toContain('2'); | ||
clickToCount.click(); | ||
clickToCount.click(); | ||
expect(clickToCount.textContent).toContain('4'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function clickToCountActivity(): HTMLButtonElement { | ||
let count = 0; | ||
|
||
const clickToCount = document.createElement('button'); | ||
clickToCount.addEventListener('click', handleClick); | ||
updateButtonText(); | ||
|
||
function handleClick() { | ||
count += 1; | ||
updateButtonText(); | ||
} | ||
|
||
function updateButtonText() { | ||
clickToCount.textContent = `I've been clicked ${count} times`; | ||
} | ||
|
||
return clickToCount; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { mountClickToCount } from './click-to-count.mount'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { MountActivityFn } from '../activities.types'; | ||
import { mountClickToCount } from '../click-to-count'; | ||
|
||
import template from './template.html'; | ||
|
||
export const mountReadHome: MountActivityFn = function ( | ||
mountPoint: HTMLElement | ||
) { | ||
mountPoint.innerHTML = template; | ||
|
||
mountClickToCount(mountPoint); | ||
|
||
return mountPoint; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import { MountActivityFn } from '../activities.types'; | ||
import { mountClickToCount } from '../click-to-count'; | ||
|
||
import template from './template.html'; | ||
|
||
export const mountReadTechnologyUsed: MountActivityFn = function ( | ||
mountPoint: HTMLElement | ||
) { | ||
mountPoint.innerHTML = template; | ||
|
||
mountClickToCount(mountPoint); | ||
|
||
return mountPoint; | ||
}; |