From b5c038af9ee581c0852da05c330f284b601e02f5 Mon Sep 17 00:00:00 2001 From: Ken Date: Mon, 18 Feb 2019 20:59:56 -0800 Subject: [PATCH] added small exercise for jest testing --- index.html | 2 +- step2-04/README.md | 11 ++++++++++ step2-04/{ => demo}/index.html | 0 step2-04/demo/src/index.spec.ts | 32 +++++++++++++++++++++++++++++ step2-04/demo/src/index.ts | 17 +++++++++++++++ step2-04/demo/src/store/index.ts | 14 +++++++++++++ step2-04/exercise/src/stack.spec.ts | 16 +++++++++++++++ step2-04/exercise/src/stack.ts | 1 + step2-04/index.spec.ts | 16 --------------- step2-04/index.ts | 3 --- 10 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 step2-04/README.md rename step2-04/{ => demo}/index.html (100%) create mode 100644 step2-04/demo/src/index.spec.ts create mode 100644 step2-04/demo/src/index.ts create mode 100644 step2-04/demo/src/store/index.ts create mode 100644 step2-04/exercise/src/stack.spec.ts create mode 100644 step2-04/exercise/src/stack.ts delete mode 100644 step2-04/index.spec.ts delete mode 100644 step2-04/index.ts diff --git a/index.html b/index.html index 0a32121..8729d1b 100644 --- a/index.html +++ b/index.html @@ -81,7 +81,7 @@
  • - + Step 3
    UI Fabric: Theming and Styling
    diff --git a/step2-04/README.md b/step2-04/README.md new file mode 100644 index 0000000..b7197b4 --- /dev/null +++ b/step2-04/README.md @@ -0,0 +1,11 @@ +# Step 2.4 + +Testing Typescript code with jest. + +# Exercise + +1. copy the generic `Stack` code you have developed in Step 2.1 + +2. import the `Stack` class into `stack.spec.ts` + +3. Follow the instructions inside the file to complete the two tests diff --git a/step2-04/index.html b/step2-04/demo/index.html similarity index 100% rename from step2-04/index.html rename to step2-04/demo/index.html diff --git a/step2-04/demo/src/index.spec.ts b/step2-04/demo/src/index.spec.ts new file mode 100644 index 0000000..87620da --- /dev/null +++ b/step2-04/demo/src/index.spec.ts @@ -0,0 +1,32 @@ +import { square } from '.'; + +describe('jest example', () => { + beforeEach(() => { + jest.resetModules(); + }); + + it('should be able to give the square of two numbers', () => { + console.log('test'); + expect(square(5)).toBe(25); + }); + + it('should increment counter', () => { + const { increment } = require('.'); + expect(increment()).toBe(1); + }); + + it('should decrement counter', () => { + const { decrement } = require('.'); + expect(decrement()).toBe(-1); + }); + + it('should retrieve count', () => { + const { decrement, getCount, increment } = require('.'); + increment(); + increment(); + decrement(); + increment(); + + expect(getCount()).toBe(2); + }); +}); diff --git a/step2-04/demo/src/index.ts b/step2-04/demo/src/index.ts new file mode 100644 index 0000000..0ac7fff --- /dev/null +++ b/step2-04/demo/src/index.ts @@ -0,0 +1,17 @@ +let counter = 0; + +export function getCount() { + return counter; +} + +export function increment() { + return ++counter; +} + +export function decrement() { + return --counter; +} + +export function square(x: number) { + return x * x; +} diff --git a/step2-04/demo/src/store/index.ts b/step2-04/demo/src/store/index.ts new file mode 100644 index 0000000..221b5f4 --- /dev/null +++ b/step2-04/demo/src/store/index.ts @@ -0,0 +1,14 @@ +export type FilterTypes = 'all' | 'active' | 'completed'; + +export interface TodoItem { + label: string; + completed: boolean; +} + +export interface Store { + todos: { + [id: string]: TodoItem; + }; + + filter: FilterTypes; +} diff --git a/step2-04/exercise/src/stack.spec.ts b/step2-04/exercise/src/stack.spec.ts new file mode 100644 index 0000000..8b89b72 --- /dev/null +++ b/step2-04/exercise/src/stack.spec.ts @@ -0,0 +1,16 @@ +describe('stack', () => { + it('should push item to the top of the stack', () => { + // implement test here: + // 1. require the stack + // 2. create stack push calls to place some items in the stack + // 3. write assertions via the expect() API + }); + + it('should pop the item from the top of stack', () => { + // implement test here: + // 1. require the stack + // 2. create stack push calls to place some items in the stack + // 3. pop a few items off the stack + // 4. write assertions via the expect() API + }); +}); diff --git a/step2-04/exercise/src/stack.ts b/step2-04/exercise/src/stack.ts new file mode 100644 index 0000000..52c8311 --- /dev/null +++ b/step2-04/exercise/src/stack.ts @@ -0,0 +1 @@ +// Place your implementation from Step 2-01 exercise here diff --git a/step2-04/index.spec.ts b/step2-04/index.spec.ts deleted file mode 100644 index 2b48abb..0000000 --- a/step2-04/index.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { square } from '.'; - -describe('square', () => { - beforeEach(() => { - console.log('this happens before each test'); - }); - - afterEach(() => { - console.log('this happens after each test'); - }); - - it('should be able to give the square of two numbers', () => { - console.log('test'); - expect(square(5)).toBe(25); - }); -}); diff --git a/step2-04/index.ts b/step2-04/index.ts deleted file mode 100644 index 4d99754..0000000 --- a/step2-04/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function square(x: number) { - return x * x; -}