-
Notifications
You must be signed in to change notification settings - Fork 5
CounterStuffByWangJing #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a2bb4f0
43e1e6e
49e8c44
c22898d
f436d27
425952e
cfce5b6
b34821f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,5 +94,8 @@ | |
| } | ||
| } | ||
| } | ||
| }, | ||
| "cli": { | ||
| "analytics": false | ||
| } | ||
| } | ||
Large diffs are not rendered by default.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <button (click)="onAdd()">Add counter</button> | ||
|
|
||
| <div *ngFor="let counter of counters; index as i;"> | ||
| <app-counter [number]="counter.num" (change)="counter.num = $event"> </app-counter> | ||
| <button (click)="onDelete(i)">Delete</button> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: rename this button to Remove instead of Delete, to keep the same name with requirement. |
||
| </div> | ||
|
|
||
| <div> | ||
| Sum: {{sum}} | ||
| <button (click)="onResetAll()">Reset All</button> | ||
| </div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
|
||
| import { CounterGroupComponent } from './counter-group.component'; | ||
| import { CounterComponent} from '../counter/counter.component'; | ||
| /*import的時候把路徑向上走一層,加兩個點 */ | ||
|
|
||
| describe('CounterGroupComponent', () => { | ||
| let component: CounterGroupComponent; | ||
| let fixture: ComponentFixture<CounterGroupComponent>; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [CounterGroupComponent, CounterComponent] | ||
| }); | ||
| fixture = TestBed.createComponent(CounterGroupComponent); | ||
| component = fixture.componentInstance; | ||
| fixture.detectChanges(); | ||
| }); | ||
|
|
||
| it('should create', () => { | ||
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should add a counter when call onAdd', ()=>{ | ||
| component.counters = [{num:1}] | ||
| component.onAdd() | ||
| expect(component.counters).toEqual([{num:1},{num:0}]) | ||
| }) | ||
|
|
||
| it('should remove a counter when call onDelete', ()=>{ | ||
| component.counters = [{num:1},{num:0}] | ||
| component.onDelete(0) | ||
| expect(component.counters).toEqual([{num:0}]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well: verify counters when test remove feature |
||
| }) | ||
|
|
||
| it('should reset all counters and reset sum when call onResetAll', ()=>{ | ||
| component.counters = [{num:7},{num:7}] | ||
| component.onResetAll() | ||
| component.counters.forEach(element => | ||
| expect(element.num).toEqual(0)) | ||
| expect(component.sum).toEqual(0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: verify for sum should be in another test, this test for reset feature only need verify counters change |
||
| }) | ||
|
|
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { Component } from '@angular/core'; | ||
| import { reduce } from 'rxjs'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-counter-group', | ||
| templateUrl: './counter-group.component.html', | ||
| styleUrls: ['./counter-group.component.css'] | ||
| }) | ||
| export class CounterGroupComponent { | ||
| counters: {num: number}[] = [ | ||
| {num: 1}, | ||
| {num: 2}, | ||
| {num: 3} | ||
| ] | ||
|
|
||
| get sum(){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WangKe: |
||
| return this.counters.reduce( | ||
| (result, current) => result + current.num, | ||
| 0) /*这里0是reduce的初始值*/ | ||
| /* result是用于迭代计数的变量*/ | ||
| } | ||
|
|
||
| onAdd(){ | ||
| this.counters.push({num: 0}) | ||
| } | ||
|
|
||
| onResetAll(){ | ||
| this.counters.forEach(element => { | ||
| element.num = 0 | ||
| }) | ||
| } | ||
|
|
||
| onDelete(index:number){ | ||
| this.counters.splice(index, 1); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .button{ | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <div> | ||
| <button (click)="onIncrease()">+</button> | ||
| <span>number: {{number}}</span> | ||
| <button (click)="onDecrease()">-</button> | ||
| <button (click)="onResetOneCounter()">Reset</button> | ||
| </div> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
|
||
| import { CounterComponent } from './counter.component'; | ||
|
|
||
| describe('CounterComponent', () => { | ||
| let component: CounterComponent; | ||
| let fixture: ComponentFixture<CounterComponent>; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [CounterComponent] | ||
| }); | ||
| fixture = TestBed.createComponent(CounterComponent); | ||
| component = fixture.componentInstance; | ||
| fixture.detectChanges(); | ||
| }); | ||
|
|
||
| it('should create', () => { | ||
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should increase number when call OnIncrease', ()=>{ | ||
| component.number = 2 | ||
| component.onIncrease() | ||
| expect(component.number).toEqual(3) | ||
| }) | ||
|
|
||
| it('should decrease number when call OnDecrease', ()=>{ | ||
| component.number = 2 | ||
| component.onDecrease() | ||
| expect(component.number).toEqual(1) | ||
| }) | ||
|
|
||
| it('should reset number when call OnResetOneCounter', ()=>{ | ||
| component.number = 2 | ||
| component.onResetOneCounter() | ||
| expect(component.number).toEqual(0) | ||
| }) | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { Component, EventEmitter, Input, Output } from '@angular/core'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-counter', | ||
| templateUrl: './counter.component.html', | ||
| styleUrls: ['./counter.component.css'] | ||
| }) | ||
| export class CounterComponent { | ||
| @Input() number = 0 /* @Input 标记number是可以被外界设置的 */ | ||
| @Output() change = new EventEmitter() /* @Output 标记change是向外发射的事件 */ | ||
| /* 在html里,(change)="counter.num = $event",其中$event的值就是change发出来的 */ | ||
|
|
||
| onIncrease(){ | ||
| this.number ++ | ||
| this.change.emit(this.number) | ||
| } | ||
| onDecrease(){ | ||
| this.number -- | ||
| this.change.emit(this.number) | ||
| } | ||
| onResetOneCounter(){ | ||
| this.number = 0 | ||
| this.change.emit(this.number) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: add "Counter Group" title on top of "Add counter" button