mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
added small exercise for jest testing
This commit is contained in:
@@ -81,7 +81,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="Tile">
|
<li class="Tile">
|
||||||
<a target="_blank" href="/step2-03/" class="Tile-link">
|
<a target="_blank" href="/step2-03/demo/" class="Tile-link">
|
||||||
Step 3<br />
|
Step 3<br />
|
||||||
UI Fabric: Theming and Styling
|
UI Fabric: Theming and Styling
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
11
step2-04/README.md
Normal file
11
step2-04/README.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Step 2.4
|
||||||
|
|
||||||
|
Testing Typescript code with jest.
|
||||||
|
|
||||||
|
# Exercise
|
||||||
|
|
||||||
|
1. copy the generic `Stack<T>` code you have developed in Step 2.1
|
||||||
|
|
||||||
|
2. import the `Stack<T>` class into `stack.spec.ts`
|
||||||
|
|
||||||
|
3. Follow the instructions inside the file to complete the two tests
|
||||||
32
step2-04/demo/src/index.spec.ts
Normal file
32
step2-04/demo/src/index.spec.ts
Normal file
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
17
step2-04/demo/src/index.ts
Normal file
17
step2-04/demo/src/index.ts
Normal file
@@ -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;
|
||||||
|
}
|
||||||
14
step2-04/demo/src/store/index.ts
Normal file
14
step2-04/demo/src/store/index.ts
Normal file
@@ -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;
|
||||||
|
}
|
||||||
16
step2-04/exercise/src/stack.spec.ts
Normal file
16
step2-04/exercise/src/stack.spec.ts
Normal file
@@ -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
|
||||||
|
});
|
||||||
|
});
|
||||||
1
step2-04/exercise/src/stack.ts
Normal file
1
step2-04/exercise/src/stack.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// Place your implementation from Step 2-01 exercise here
|
||||||
@@ -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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export function square(x: number) {
|
|
||||||
return x * x;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user