├── src ├── assets │ └── .gitkeep ├── app │ ├── app.component.css │ ├── components │ │ ├── list │ │ │ ├── list.component.css │ │ │ ├── list.component.spec.ts │ │ │ ├── list.component.html │ │ │ └── list.component.ts │ │ ├── list-item │ │ │ ├── list-item.component.css │ │ │ ├── list-item.component.html │ │ │ ├── list-item.component.ts │ │ │ └── list-item.component.spec.ts │ │ └── item-detail │ │ │ ├── item-detail.component.css │ │ │ ├── item-detail.component.ts │ │ │ ├── item-detail.component.html │ │ │ └── item-detail.component.spec.ts │ ├── model │ │ ├── item-filter.enum.ts │ │ ├── item-sort.ts │ │ ├── todo-item.ts │ │ ├── date-time-seconds.ts │ │ └── todo-item.functions.ts │ ├── app.component.html │ ├── app.component.ts │ ├── data │ │ ├── mock-data.ts │ │ ├── fake-backend.service.ts │ │ └── fake-backend.service.spec.ts │ ├── pipe │ │ ├── seconds-to-date.pipe.spec.ts │ │ └── seconds-to-date.pipe.ts │ ├── shared │ │ └── material │ │ │ ├── material.module.spec.ts │ │ │ └── material.module.ts │ ├── app-routing.module.ts │ ├── service │ │ ├── todo-item.service.spec.ts │ │ ├── todo-item-storage.ts │ │ ├── todo-item-storage.mock.ts │ │ └── todo-item.service.ts │ ├── app.component.spec.ts │ └── app.module.ts ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── styles.css ├── tsconfig.app.json ├── tsconfig.spec.json ├── tslint.json ├── browserslist ├── main.ts ├── index.html ├── test.ts ├── karma.conf.js └── polyfills.ts ├── e2e ├── src │ ├── app.po.ts │ └── app.e2e-spec.ts ├── tsconfig.e2e.json └── protractor.conf.js ├── .editorconfig ├── tsconfig.json ├── README.md ├── .gitignore ├── package.json ├── tslint.json └── angular.json /src/assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/app.component.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/components/list/list.component.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/components/list-item/list-item.component.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/components/item-detail/item-detail.component.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/app/model/item-filter.enum.ts: -------------------------------------------------------------------------------- 1 | export enum ItemFilter { 2 | ALL, 3 | INCOMPLETE, 4 | COMPLETE, 5 | } 6 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orjandesmet/angular-state-management-comparison/HEAD/src/favicon.ico -------------------------------------------------------------------------------- /src/styles.css: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | @import '~@angular/material/prebuilt-themes/indigo-pink.css'; 3 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

TODO List

5 |
6 |
7 | 8 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'todo-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.css'] 7 | }) 8 | export class AppComponent { 9 | } 10 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "types": [] 6 | }, 7 | "exclude": [ 8 | "test.ts", 9 | "**/*.spec.ts", 10 | "**/*.mock.ts", 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getParagraphText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/app/data/mock-data.ts: -------------------------------------------------------------------------------- 1 | import { createTodoItem, TodoItem } from '../model/todo-item'; 2 | 3 | export const MOCK_TODO_ITEMS: TodoItem[] = [ 4 | createTodoItem('1', 'Do work', new Date(2018, 9, 10)), 5 | createTodoItem('2', 'Eat cookies', new Date(2018, 8, 27), new Date()), 6 | ]; 7 | -------------------------------------------------------------------------------- /src/app/pipe/seconds-to-date.pipe.spec.ts: -------------------------------------------------------------------------------- 1 | import { SecondsToDatePipe } from './seconds-to-date.pipe'; 2 | 3 | describe('SecondsToDatePipe', () => { 4 | it('create an instance', () => { 5 | const pipe = new SecondsToDatePipe(); 6 | expect(pipe).toBeTruthy(); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /src/app/model/item-sort.ts: -------------------------------------------------------------------------------- 1 | export interface ItemSort { 2 | field: string; 3 | ascending: boolean; 4 | } 5 | 6 | export function updateItemSort(itemSort: ItemSort, field: string): ItemSort { 7 | return { 8 | ...itemSort, 9 | field, 10 | ascending: field === itemSort.field ? !itemSort.ascending : true, 11 | }; 12 | } 13 | -------------------------------------------------------------------------------- /src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "test.ts", 12 | "polyfills.ts" 13 | ], 14 | "include": [ 15 | "**/*.spec.ts", 16 | "**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /src/app/shared/material/material.module.spec.ts: -------------------------------------------------------------------------------- 1 | import { MaterialModule } from './material.module'; 2 | 3 | describe('MaterialModule', () => { 4 | let materialModule: MaterialModule; 5 | 6 | beforeEach(() => { 7 | materialModule = new MaterialModule(); 8 | }); 9 | 10 | it('should create an instance', () => { 11 | expect(materialModule).toBeTruthy(); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "todo", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "todo", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | 3 | describe('workspace-project App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getParagraphText()).toEqual('Welcome to angular-state-management-comparison!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /src/app/pipe/seconds-to-date.pipe.ts: -------------------------------------------------------------------------------- 1 | import { Pipe, PipeTransform } from '@angular/core'; 2 | import { convertSecondsToDate, DateTimeSeconds } from '../model/date-time-seconds'; 3 | 4 | @Pipe({ 5 | name: 'secondsToDate' 6 | }) 7 | export class SecondsToDatePipe implements PipeTransform { 8 | 9 | transform(dateTimeSeconds: DateTimeSeconds): Date { 10 | return convertSecondsToDate(dateTimeSeconds); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # 5 | # For IE 9-11 support, please remove 'not' from the last line of the file and adjust as needed 6 | 7 | > 0.5% 8 | last 2 versions 9 | Firefox ESR 10 | not dead 11 | not IE 9-11 -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | import 'hammerjs'; 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | 8 | if (environment.production) { 9 | enableProdMode(); 10 | } 11 | 12 | platformBrowserDynamic().bootstrapModule(AppModule) 13 | .catch(err => console.log(err)); 14 | -------------------------------------------------------------------------------- /src/app/components/list-item/list-item.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{ item.name }} 4 | 5 |
6 | 7 | info 8 | 9 | 10 | delete 11 | 12 |
13 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | AngularStateManagementComparison 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "importHelpers": true, 6 | "outDir": "./dist/out-tsc", 7 | "sourceMap": true, 8 | "declaration": false, 9 | "module": "es2015", 10 | "moduleResolution": "node", 11 | "emitDecoratorMetadata": true, 12 | "experimentalDecorators": true, 13 | "target": "es5", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2017", 19 | "dom" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { RouterModule, Routes } from '@angular/router'; 3 | import { ItemDetailComponent } from './components/item-detail/item-detail.component'; 4 | import { ListComponent } from './components/list/list.component'; 5 | 6 | const routes: Routes = [ 7 | { 8 | path: '', 9 | pathMatch: 'full', 10 | component: ListComponent, 11 | }, 12 | { 13 | path: ':id', 14 | component: ItemDetailComponent, 15 | } 16 | ]; 17 | 18 | @NgModule({ 19 | imports: [RouterModule.forRoot(routes)], 20 | exports: [RouterModule] 21 | }) 22 | export class AppRoutingModule { } 23 | -------------------------------------------------------------------------------- /src/app/model/todo-item.ts: -------------------------------------------------------------------------------- 1 | import { convertDateToSeconds } from './date-time-seconds'; 2 | 3 | export interface TodoItem { 4 | id: string; 5 | name: string; 6 | createdDateTime: { seconds: number, nanoSeconds: number }; 7 | completedDateTime?: { seconds: number, nanoSeconds: number }; 8 | } 9 | 10 | export function createTodoItem( 11 | id: string = null, name: string = '', createdDateTime: Date = new Date(), completedDateTime: Date = null 12 | ): TodoItem { 13 | return { 14 | id, 15 | name, 16 | createdDateTime: convertDateToSeconds(createdDateTime), 17 | completedDateTime: convertDateToSeconds(completedDateTime), 18 | }; 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular State Management Comparison 2 | 3 | Comparison application for state management solutions in Angular. 4 | The master branch will not have a working application, because it contains no state management solution. 5 | Checkout one of the other branches and run with `ng serve` to have an application running on `http://localhost:4200/`. 6 | 7 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 6.1.5. 8 | 9 | This [blog post](https://ordina-jworks.github.io/angular/2018/10/08/angular-state-management-comparison.html) explains the differences between the solutions. 10 | 11 | Feel free to clone or fork. 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | yarn-error.log 34 | testem.log 35 | /typings 36 | 37 | # System Files 38 | .DS_Store 39 | Thumbs.db 40 | -------------------------------------------------------------------------------- /src/app/service/todo-item.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { inject, TestBed } from '@angular/core/testing'; 2 | import { TODO_ITEM_STORAGE } from './todo-item-storage'; 3 | import { itemStorageSpy } from './todo-item-storage.mock'; 4 | import { TodoItemService } from './todo-item.service'; 5 | 6 | 7 | describe('TodoItemService', () => { 8 | beforeEach(() => { 9 | TestBed.configureTestingModule({ 10 | providers: [ 11 | TodoItemService, 12 | { provide: TODO_ITEM_STORAGE, useValue: itemStorageSpy }, 13 | ], 14 | }); 15 | }); 16 | 17 | it('should be created', inject([TodoItemService], (service: TodoItemService) => { 18 | expect(service).toBeTruthy(); 19 | })); 20 | }); 21 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build ---prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * In development mode, for easier debugging, you can ignore zone related error 11 | * stack frames such as `zone.run`/`zoneDelegate.invokeTask` by importing the 12 | * below file. Don't forget to comment it out in production mode 13 | * because it will have a performance impact when errors are thrown 14 | */ 15 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 16 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /src/app/components/list-item/list-item.component.ts: -------------------------------------------------------------------------------- 1 | import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; 2 | import { TodoItem } from '../../model/todo-item'; 3 | 4 | @Component({ 5 | selector: 'todo-list-item', 6 | templateUrl: './list-item.component.html', 7 | styleUrls: ['./list-item.component.css'], 8 | changeDetection: ChangeDetectionStrategy.OnPush, 9 | }) 10 | export class ListItemComponent { 11 | 12 | @Input() item: TodoItem; 13 | @Output() toggleItemCompleted = new EventEmitter(); 14 | @Output() delete = new EventEmitter(); 15 | 16 | onCheckboxClicked(): void { 17 | this.toggleItemCompleted.emit(this.item.id); 18 | } 19 | 20 | onDeleteClicked(): void { 21 | this.delete.emit(this.item.id); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, TestBed } from '@angular/core/testing'; 2 | import { RouterTestingModule } from '@angular/router/testing'; 3 | import { AppComponent } from './app.component'; 4 | import { MaterialModule } from './shared/material/material.module'; 5 | 6 | describe('AppComponent', () => { 7 | beforeEach(async(() => { 8 | TestBed.configureTestingModule({ 9 | imports: [ 10 | RouterTestingModule, 11 | MaterialModule, 12 | ], 13 | declarations: [ 14 | AppComponent 15 | ], 16 | }).compileComponents(); 17 | })); 18 | it('should create the app', async(() => { 19 | const fixture = TestBed.createComponent(AppComponent); 20 | const app = fixture.debugElement.componentInstance; 21 | expect(app).toBeTruthy(); 22 | })); 23 | }); 24 | -------------------------------------------------------------------------------- /src/app/service/todo-item-storage.ts: -------------------------------------------------------------------------------- 1 | import { InjectionToken } from '@angular/core'; 2 | import { Observable } from 'rxjs'; 3 | import { ItemFilter } from '../model/item-filter.enum'; 4 | import { ItemSort } from '../model/item-sort'; 5 | import { TodoItem } from '../model/todo-item'; 6 | 7 | export interface TodoItemStorage { 8 | 9 | filter$: Observable; 10 | sort$: Observable; 11 | allItems$: Observable; 12 | 13 | loadItems(): void; 14 | getItem(id: string): Observable; 15 | addItem(item: Partial): void; 16 | removeItem(id: string): void; 17 | toggleItemCompleted(id: string): void; 18 | setSortField(field: string): void; 19 | setFilter(filter: ItemFilter): void; 20 | } 21 | 22 | export const TODO_ITEM_STORAGE = new InjectionToken('todo.item.storage'); 23 | -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: require('path').join(__dirname, './tsconfig.e2e.json') 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; -------------------------------------------------------------------------------- /src/app/shared/material/material.module.ts: -------------------------------------------------------------------------------- 1 | import { CommonModule } from '@angular/common'; 2 | import { NgModule } from '@angular/core'; 3 | import { MatButtonModule, MatButtonToggleModule, MatCardModule, MatCheckboxModule, MatFormFieldModule, MatIconModule, MatInputModule, MatToolbarModule } from '@angular/material'; 4 | 5 | @NgModule({ 6 | imports: [ 7 | CommonModule, 8 | MatCardModule, 9 | MatToolbarModule, 10 | MatButtonModule, 11 | MatFormFieldModule, 12 | MatInputModule, 13 | MatCheckboxModule, 14 | MatButtonToggleModule, 15 | MatIconModule, 16 | ], 17 | exports: [ 18 | MatCardModule, 19 | MatToolbarModule, 20 | MatButtonModule, 21 | MatFormFieldModule, 22 | MatInputModule, 23 | MatCheckboxModule, 24 | MatButtonToggleModule, 25 | MatIconModule, 26 | ], 27 | declarations: [] 28 | }) 29 | export class MaterialModule { } 30 | -------------------------------------------------------------------------------- /src/app/components/item-detail/item-detail.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | import { ActivatedRoute, Params, Router } from '@angular/router'; 3 | import { filter, pluck, switchMap } from 'rxjs/operators'; 4 | import { TodoItemService } from '../../service/todo-item.service'; 5 | 6 | @Component({ 7 | selector: 'todo-item-detail', 8 | templateUrl: './item-detail.component.html', 9 | styleUrls: ['./item-detail.component.css'] 10 | }) 11 | export class ItemDetailComponent { 12 | 13 | item$ = this.route.params.pipe( 14 | pluck('id'), 15 | filter(id => !!id), 16 | switchMap(id => this.service.getItem(id)), 17 | ); 18 | 19 | constructor( 20 | private route: ActivatedRoute, 21 | private router: Router, 22 | private service: TodoItemService, 23 | ) { } 24 | 25 | onCheckboxClicked(id: string) { 26 | this.service.toggleItemCompleted(id); 27 | } 28 | 29 | removeItem(id: string) { 30 | this.service.removeItem(id); 31 | this.router.navigateByUrl('/'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage'), 20 | reports: ['html', 'lcovonly'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false 30 | }); 31 | }; -------------------------------------------------------------------------------- /src/app/model/date-time-seconds.ts: -------------------------------------------------------------------------------- 1 | import { isNullOrUndefined } from 'util'; 2 | 3 | export interface DateTimeSeconds { 4 | seconds: number; 5 | nanoSeconds: number; 6 | } 7 | 8 | export function createDateTimeSeconds(seconds: number = 0, nanoSeconds: number = 0): DateTimeSeconds { 9 | return { 10 | seconds, 11 | nanoSeconds, 12 | }; 13 | } 14 | 15 | export function convertDateToSeconds(dateTime: Date): DateTimeSeconds { 16 | if (isNullOrUndefined(dateTime)) { 17 | return null; 18 | } else { 19 | const timeInMS = dateTime.getTime(); 20 | return createDateTimeSeconds(timeInMS / 1000, timeInMS % 1000 * 1000); 21 | } 22 | } 23 | 24 | export function convertSecondsToMS(dateTimeSeconds: DateTimeSeconds): number { 25 | if (isNullOrUndefined(dateTimeSeconds)) { 26 | return null; 27 | } 28 | return dateTimeSeconds.seconds * 1000 + dateTimeSeconds.nanoSeconds / 1000; 29 | } 30 | 31 | export function convertSecondsToDate(dateTimeSeconds: DateTimeSeconds): Date { 32 | if (isNullOrUndefined(dateTimeSeconds)) { 33 | return null; 34 | } 35 | const timeInMS = convertSecondsToMS(dateTimeSeconds); 36 | return new Date(timeInMS); 37 | } 38 | -------------------------------------------------------------------------------- /src/app/components/list-item/list-item.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | import { NoopAnimationsModule } from '@angular/platform-browser/animations'; 3 | import { RouterTestingModule } from '@angular/router/testing'; 4 | import { createTodoItem } from '../../model/todo-item'; 5 | import { MaterialModule } from '../../shared/material/material.module'; 6 | import { ListItemComponent } from './list-item.component'; 7 | 8 | describe('ListItemComponent', () => { 9 | let component: ListItemComponent; 10 | let fixture: ComponentFixture; 11 | 12 | beforeEach(async(() => { 13 | TestBed.configureTestingModule({ 14 | imports: [NoopAnimationsModule, MaterialModule, RouterTestingModule], 15 | declarations: [ ListItemComponent ], 16 | }) 17 | .compileComponents(); 18 | })); 19 | 20 | beforeEach(() => { 21 | fixture = TestBed.createComponent(ListItemComponent); 22 | component = fixture.componentInstance; 23 | component.item = createTodoItem('TEST_ID'); 24 | fixture.detectChanges(); 25 | }); 26 | 27 | it('should create', () => { 28 | expect(component).toBeTruthy(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { FlexLayoutModule } from '@angular/flex-layout'; 3 | import { ReactiveFormsModule } from '@angular/forms'; 4 | import { BrowserModule } from '@angular/platform-browser'; 5 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 6 | import { AppRoutingModule } from './app-routing.module'; 7 | import { AppComponent } from './app.component'; 8 | import { ItemDetailComponent } from './components/item-detail/item-detail.component'; 9 | import { ListItemComponent } from './components/list-item/list-item.component'; 10 | import { ListComponent } from './components/list/list.component'; 11 | import { SecondsToDatePipe } from './pipe/seconds-to-date.pipe'; 12 | import { MaterialModule } from './shared/material/material.module'; 13 | 14 | @NgModule({ 15 | declarations: [ 16 | AppComponent, 17 | ListComponent, 18 | ListItemComponent, 19 | ItemDetailComponent, 20 | SecondsToDatePipe 21 | ], 22 | imports: [ 23 | BrowserModule, 24 | BrowserAnimationsModule, 25 | ReactiveFormsModule, 26 | AppRoutingModule, 27 | MaterialModule, 28 | FlexLayoutModule, 29 | ], 30 | providers: [], 31 | bootstrap: [AppComponent] 32 | }) 33 | export class AppModule { } 34 | -------------------------------------------------------------------------------- /src/app/service/todo-item-storage.mock.ts: -------------------------------------------------------------------------------- 1 | import { of } from 'rxjs'; 2 | import { tap } from 'rxjs/operators'; 3 | import { MOCK_TODO_ITEMS } from '../data/mock-data'; 4 | import { ItemFilter } from '../model/item-filter.enum'; 5 | import { ItemSort } from '../model/item-sort'; 6 | import { TodoItem } from '../model/todo-item'; 7 | import { TodoItemStorage } from './todo-item-storage'; 8 | 9 | export const itemStorageSpy: TodoItemStorage & { allItemsSpy: jasmine.Spy, filterSpy: jasmine.Spy, sortSpy: jasmine.Spy } = { 10 | allItems$: of(MOCK_TODO_ITEMS).pipe(tap(items => itemStorageSpy.allItemsSpy(items))), 11 | sort$: of({field: name, ascending: true}).pipe(tap(sort => itemStorageSpy.sortSpy(sort))), 12 | filter$: of(ItemFilter.ALL).pipe(tap(filter => itemStorageSpy.filterSpy(filter))), 13 | loadItems: jasmine.createSpy('loadItems'), 14 | allItemsSpy: jasmine.createSpy('allItems'), 15 | sortSpy: jasmine.createSpy('sort'), 16 | filterSpy: jasmine.createSpy('filter'), 17 | getItem: jasmine.createSpy('getItem'), 18 | addItem: jasmine.createSpy('addItem'), 19 | removeItem: jasmine.createSpy('removeItem'), 20 | setFilter: jasmine.createSpy('setFilter'), 21 | setSortField: jasmine.createSpy('setSortField'), 22 | toggleItemCompleted: jasmine.createSpy('toggleItemCompleted'), 23 | }; 24 | -------------------------------------------------------------------------------- /src/app/components/item-detail/item-detail.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{ item.name }} 5 | 6 | 7 |
Created: {{ item.createdDateTime | secondsToDate | date:'dd/MM/yyyy HH:mm:ss' }}
8 |
Completed: {{ item.completedDateTime | secondsToDate | date:'dd/MM/yyyy HH:mm:ss' }}
9 |
10 | 11 | 12 | navigate_before 13 | BACK 14 | 15 | 19 | 20 |
21 |
22 | 23 | 24 | Item does not exist 25 | 26 | 27 | navigate_before 28 | BACK 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/app/components/list/list.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { NO_ERRORS_SCHEMA } from '@angular/core'; 2 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 3 | import { ReactiveFormsModule } from '@angular/forms'; 4 | import { NoopAnimationsModule } from '@angular/platform-browser/animations'; 5 | import { TODO_ITEM_STORAGE } from '../../service/todo-item-storage'; 6 | import { itemStorageSpy } from '../../service/todo-item-storage.mock'; 7 | import { TodoItemService } from '../../service/todo-item.service'; 8 | import { MaterialModule } from '../../shared/material/material.module'; 9 | import { ListComponent } from './list.component'; 10 | 11 | 12 | describe('ListComponent', () => { 13 | let component: ListComponent; 14 | let fixture: ComponentFixture; 15 | 16 | beforeEach(async(() => { 17 | TestBed.configureTestingModule({ 18 | imports: [NoopAnimationsModule, MaterialModule, ReactiveFormsModule], 19 | declarations: [ ListComponent ], 20 | providers: [ 21 | TodoItemService, 22 | { provide: TODO_ITEM_STORAGE, useValue: itemStorageSpy } 23 | ], 24 | schemas: [NO_ERRORS_SCHEMA], 25 | }) 26 | .compileComponents(); 27 | })); 28 | 29 | beforeEach(() => { 30 | fixture = TestBed.createComponent(ListComponent); 31 | component = fixture.componentInstance; 32 | fixture.detectChanges(); 33 | }); 34 | 35 | it('should create', () => { 36 | expect(component).toBeTruthy(); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-state-management-comparison", 3 | "version": "1.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "^7.2.0", 15 | "@angular/cdk": "^7.2.1", 16 | "@angular/common": "^7.2.0", 17 | "@angular/compiler": "^7.2.0", 18 | "@angular/core": "^7.2.0", 19 | "@angular/flex-layout": "^7.0.0-beta.22", 20 | "@angular/forms": "^7.2.0", 21 | "@angular/http": "^7.2.0", 22 | "@angular/material": "^7.2.1", 23 | "@angular/platform-browser": "^7.2.0", 24 | "@angular/platform-browser-dynamic": "^7.2.0", 25 | "@angular/router": "^7.2.0", 26 | "core-js": "^2.6.1", 27 | "hammerjs": "^2.0.8", 28 | "rxjs": "^6.3.3", 29 | "tslib": "^1.9.0", 30 | "zone.js": "~0.8.26" 31 | }, 32 | "devDependencies": { 33 | "@angular-devkit/build-angular": "~0.12.0", 34 | "@angular/cli": "7.2.1", 35 | "@angular/compiler-cli": "^7.2.0", 36 | "@angular/language-service": "^7.2.0", 37 | "@types/jasmine": "~3.3.5", 38 | "@types/jasminewd2": "~2.0.6", 39 | "@types/node": "~10.12.18", 40 | "codelyzer": "~4.5.0", 41 | "jasmine-core": "~3.3.0", 42 | "jasmine-spec-reporter": "~4.2.1", 43 | "karma": "^3.0.0", 44 | "karma-chrome-launcher": "~2.2.0", 45 | "karma-coverage-istanbul-reporter": "~2.0.4", 46 | "karma-jasmine": "~2.0.1", 47 | "karma-jasmine-html-reporter": "^1.4.0", 48 | "protractor": "~5.4.2", 49 | "ts-node": "~7.0.1", 50 | "tslint": "~5.12.0", 51 | "typescript": "~3.2.2" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/app/components/list/list.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 | 6 |
7 |
8 |
9 | Sort by: 10 | 11 | 12 | {{ sortableField.label }} arrow_drop_uparrow_drop_down 13 | 14 | 15 |
16 |
17 | Filter: 18 | 19 | 20 | All 21 | 22 | 23 | Complete 24 | 25 | 26 | Incomplete 27 | 28 | 29 |
30 |
31 | 32 | 33 | 34 | 35 | 36 | Nothing found with this filter 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/app/components/item-detail/item-detail.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | import { NoopAnimationsModule } from '@angular/platform-browser/animations'; 3 | import { ActivatedRoute, Params } from '@angular/router'; 4 | import { RouterTestingModule } from '@angular/router/testing'; 5 | import { Subject } from 'rxjs'; 6 | import { SecondsToDatePipe } from '../../pipe/seconds-to-date.pipe'; 7 | import { TODO_ITEM_STORAGE } from '../../service/todo-item-storage'; 8 | import { itemStorageSpy } from '../../service/todo-item-storage.mock'; 9 | import { TodoItemService } from '../../service/todo-item.service'; 10 | import { MaterialModule } from '../../shared/material/material.module'; 11 | import { ItemDetailComponent } from './item-detail.component'; 12 | 13 | 14 | const routeMock = { 15 | params: new Subject(), 16 | }; 17 | 18 | describe('ItemDetailComponent', () => { 19 | let component: ItemDetailComponent; 20 | let fixture: ComponentFixture; 21 | 22 | beforeEach(async(() => { 23 | TestBed.configureTestingModule({ 24 | imports: [NoopAnimationsModule, MaterialModule, RouterTestingModule], 25 | declarations: [ ItemDetailComponent, SecondsToDatePipe ], 26 | providers: [ 27 | TodoItemService, 28 | { provide: TODO_ITEM_STORAGE, useValue: itemStorageSpy }, 29 | { provide: ActivatedRoute, useValue: routeMock }, 30 | ] 31 | }) 32 | .compileComponents(); 33 | })); 34 | 35 | beforeEach(() => { 36 | fixture = TestBed.createComponent(ItemDetailComponent); 37 | component = fixture.componentInstance; 38 | routeMock.params.next({id: 'TEST_ID'}); 39 | fixture.detectChanges(); 40 | }); 41 | 42 | it('should create', () => { 43 | expect(component).toBeTruthy(); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /src/app/model/todo-item.functions.ts: -------------------------------------------------------------------------------- 1 | import { isNullOrUndefined } from 'util'; 2 | import { convertSecondsToMS } from './date-time-seconds'; 3 | import { ItemFilter } from './item-filter.enum'; 4 | import { ItemSort } from './item-sort'; 5 | import { TodoItem } from './todo-item'; 6 | 7 | export function randomId(usedIds: string[] = []): string { 8 | let id = generateRandomId(); 9 | while (usedIds.includes(id)) { 10 | id = generateRandomId(); 11 | } 12 | return id; 13 | } 14 | function generateRandomId(): string  { 15 | return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10); 16 | } 17 | 18 | export const filterTodoItemBy = (filter: ItemFilter) => (item: TodoItem): boolean => { 19 | switch (filter) { 20 | case ItemFilter.ALL: 21 | return true; 22 | case ItemFilter.COMPLETE: 23 | return !isNullOrUndefined(item.completedDateTime); 24 | case ItemFilter.INCOMPLETE: 25 | return isNullOrUndefined(item.completedDateTime); 26 | default: 27 | return true; 28 | } 29 | }; 30 | 31 | export const sortTodoItemBy = (itemSort: ItemSort) => (a: TodoItem, b: TodoItem): number => { 32 | let sortNumber: number; 33 | switch (itemSort.field) { 34 | case 'name': 35 | sortNumber = a.name.toLowerCase().localeCompare(b.name.toLowerCase()); 36 | break; 37 | case 'createdDateTime': 38 | sortNumber = convertSecondsToMS(a.createdDateTime) - convertSecondsToMS(b.createdDateTime); 39 | break; 40 | default: 41 | sortNumber = 1; 42 | } 43 | if (!itemSort.ascending) { 44 | return sortNumber * -1; 45 | } 46 | return sortNumber; 47 | }; 48 | 49 | export function sortAndFilterData(data: TodoItem[], filter: ItemFilter, itemSort: ItemSort): TodoItem[] { 50 | return data 51 | .filter(filterTodoItemBy(filter)) 52 | .sort(sortTodoItemBy(itemSort)); 53 | } 54 | -------------------------------------------------------------------------------- /src/app/service/todo-item.service.ts: -------------------------------------------------------------------------------- 1 | import { Inject, Injectable } from '@angular/core'; 2 | import { Observable } from 'rxjs'; 3 | import { map } from 'rxjs/operators'; 4 | import { ItemFilter } from '../model/item-filter.enum'; 5 | import { ItemSort } from '../model/item-sort'; 6 | import { TodoItem } from '../model/todo-item'; 7 | import { TodoItemStorage, TODO_ITEM_STORAGE } from './todo-item-storage'; 8 | 9 | @Injectable({ 10 | providedIn: 'root' 11 | }) 12 | export class TodoItemService { 13 | 14 | todoItems$: Observable = this.storage.allItems$; 15 | sort$: Observable = this.storage.sort$; 16 | filter$: Observable = this.storage.filter$.pipe(map( 17 | filter => { 18 | switch (filter) { 19 | case ItemFilter.COMPLETE: 20 | return 'complete'; 21 | case ItemFilter.INCOMPLETE: 22 | return 'incomplete'; 23 | case ItemFilter.ALL: 24 | default: 25 | return 'all'; 26 | } 27 | } 28 | )); 29 | 30 | constructor( 31 | @Inject(TODO_ITEM_STORAGE) private storage: TodoItemStorage 32 | ) { } 33 | 34 | loadItems(): void { 35 | return this.storage.loadItems(); 36 | } 37 | 38 | getItem(id: string): Observable { 39 | return this.storage.getItem(id); 40 | } 41 | 42 | addItem(item: Partial) { 43 | this.storage.addItem(item); 44 | } 45 | 46 | removeItem(id: string) { 47 | this.storage.removeItem(id); 48 | } 49 | 50 | toggleItemCompleted(id: string) { 51 | this.storage.toggleItemCompleted(id); 52 | } 53 | 54 | setSortField(field: string) { 55 | this.storage.setSortField(field); 56 | } 57 | 58 | setFilterString(stringValue: string) { 59 | switch (stringValue) { 60 | case 'incomplete': 61 | this.storage.setFilter(ItemFilter.INCOMPLETE); 62 | break; 63 | case 'complete': 64 | this.storage.setFilter(ItemFilter.COMPLETE); 65 | break; 66 | case 'all': 67 | default: 68 | this.storage.setFilter(ItemFilter.ALL); 69 | break; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/app/components/list/list.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 3 | import { MatButtonToggleChange } from '@angular/material'; 4 | import { Observable } from 'rxjs'; 5 | import { map } from 'rxjs/operators'; 6 | import { ItemSort } from '../../model/item-sort'; 7 | import { TodoItem } from '../../model/todo-item'; 8 | import { TodoItemService } from '../../service/todo-item.service'; 9 | 10 | @Component({ 11 | selector: 'todo-list', 12 | templateUrl: './list.component.html', 13 | styleUrls: ['./list.component.css'] 14 | }) 15 | export class ListComponent implements OnInit { 16 | addFormGroup: FormGroup = this.createFormGroup(); 17 | todoItems$: Observable = this.service.todoItems$; 18 | hasTodoItems$: Observable = this.todoItems$.pipe(map(items => items.length > 0)); 19 | sort$: Observable = this.service.sort$; 20 | filter$: Observable = this.service.filter$; 21 | sortableFields = [{key: 'name', label: 'Name'}, {key: 'createdDateTime', label: 'Created Date & Time'}]; 22 | 23 | constructor( 24 | private fb: FormBuilder, 25 | private service: TodoItemService 26 | ) { } 27 | 28 | ngOnInit() { 29 | this.service.loadItems(); 30 | } 31 | 32 | toggleItemCompleted(id: string): void { 33 | this.service.toggleItemCompleted(id); 34 | } 35 | 36 | submit() { 37 | if (this.addFormGroup.valid) { 38 | this.service.addItem(this.addFormGroup.value); 39 | this.addFormGroup.get('name').setValue(''); 40 | this.addFormGroup.get('name').markAsPristine(); 41 | this.addFormGroup.get('name').markAsUntouched(); 42 | this.addFormGroup.get('name').updateValueAndValidity(); 43 | } 44 | } 45 | 46 | deleteTodoItem(id: string): void { 47 | this.service.removeItem(id); 48 | } 49 | 50 | onFilterChange(event: MatButtonToggleChange) { 51 | this.service.setFilterString(event.value); 52 | } 53 | 54 | onSortChange(value: string) { 55 | this.service.setSortField(value); 56 | } 57 | 58 | private createFormGroup(): FormGroup { 59 | return this.fb.group({ 60 | name: this.fb.control('', Validators.required) 61 | }); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/app/data/fake-backend.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Observable, of, throwError } from 'rxjs'; 3 | import { DateTimeSeconds } from '../model/date-time-seconds'; 4 | import { createTodoItem, TodoItem } from '../model/todo-item'; 5 | import { randomId } from '../model/todo-item.functions'; 6 | import { MOCK_TODO_ITEMS } from './mock-data'; 7 | 8 | // This service mocks a REST Backend 9 | 10 | @Injectable({ 11 | providedIn: 'root' 12 | }) 13 | export class FakeBackendService { 14 | 15 | private items = MOCK_TODO_ITEMS.reduce((p, c) => ({ ...p, [c.id]: c }), {}); 16 | private ids = Object.keys(this.items); 17 | 18 | getAll(): Observable { 19 | console.log('FakeBackendService: GET All'); 20 | return of(Object.values(this.items)); 21 | } 22 | 23 | addOne(item: Partial): Observable { 24 | console.log('FakeBackendService: POST One'); 25 | const newItem: TodoItem = { 26 | ...createTodoItem(), 27 | ...item, 28 | id: item.id || randomId(this.ids), 29 | }; 30 | this.items[newItem.id] = newItem; 31 | this.ids.push(newItem.id); 32 | return of(newItem); 33 | } 34 | 35 | updateOne(id: string, item: Partial): Observable { 36 | console.log('FakeBackendService: PUT One'); 37 | const index = this.ids.indexOf(id); 38 | if (index === -1) { 39 | console.error(`FakeBackendService: PUT Id ${id} not found`); 40 | return throwError('404'); 41 | } else { 42 | delete item.id; 43 | this.items[id] = { 44 | ...this.items[id], 45 | ...item 46 | }; 47 | return of(this.items[id]); 48 | } 49 | } 50 | 51 | toggleCompleted(id: string, completedDateTime: DateTimeSeconds) { 52 | console.log('FakeBackendService: PATCH One'); 53 | const index = this.ids.indexOf(id); 54 | if (index === -1) { 55 | console.error(`FakeBackendService: PATCH Id ${id} not found`); 56 | return throwError('404'); 57 | } else { 58 | this.items[id] = { 59 | ...this.items[id], 60 | completedDateTime: this.items[id].completedDateTime ? null : completedDateTime 61 | }; 62 | return of(this.items[id]); 63 | } 64 | } 65 | 66 | removeOne(id: string): Observable { 67 | console.log('FakeBackendService: DELETE One'); 68 | const index = this.ids.indexOf(id); 69 | if (index === -1) { 70 | console.error(`FakeBackendService: DELETE Id ${id} not found`); 71 | return throwError('404'); 72 | } else { 73 | delete this.items[id]; 74 | this.ids.splice(index, 1); 75 | return of(id); 76 | } 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/weak-map'; 35 | // import 'core-js/es6/set'; 36 | 37 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 38 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 39 | 40 | /** IE10 and IE11 requires the following for the Reflect API. */ 41 | // import 'core-js/es6/reflect'; 42 | 43 | 44 | /** Evergreen browsers require these. **/ 45 | // Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove. 46 | 47 | 48 | 49 | /** 50 | * Web Animations `@angular/platform-browser/animations` 51 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 52 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 53 | **/ 54 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 55 | 56 | /** 57 | * By default, zone.js will patch all possible macroTask and DomEvents 58 | * user can disable parts of macroTask/DomEvents patch by setting following flags 59 | */ 60 | 61 | // (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 62 | // (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 63 | // (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 64 | 65 | /* 66 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 67 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 68 | */ 69 | // (window as any).__Zone_enable_cross_context_check = true; 70 | 71 | /*************************************************************************************************** 72 | * Zone JS is required by default for Angular itself. 73 | */ 74 | import 'zone.js/dist/zone'; // Included with Angular CLI. 75 | 76 | 77 | 78 | /*************************************************************************************************** 79 | * APPLICATION IMPORTS 80 | */ 81 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer" 4 | ], 5 | "rules": { 6 | "arrow-return-shorthand": true, 7 | "callable-types": true, 8 | "class-name": true, 9 | "comment-format": [ 10 | true, 11 | "check-space" 12 | ], 13 | "curly": true, 14 | "deprecation": { 15 | "severity": "warn" 16 | }, 17 | "eofline": true, 18 | "forin": true, 19 | "import-blacklist": [ 20 | true, 21 | "rxjs/Rx" 22 | ], 23 | "import-spacing": true, 24 | "indent": [ 25 | true, 26 | "spaces" 27 | ], 28 | "interface-over-type-literal": true, 29 | "label-position": true, 30 | "max-line-length": [ 31 | true, 32 | 140 33 | ], 34 | "member-access": false, 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-arg": true, 47 | "no-bitwise": true, 48 | "no-console": [ 49 | true, 50 | "debug", 51 | "info", 52 | "time", 53 | "timeEnd", 54 | "trace" 55 | ], 56 | "no-construct": true, 57 | "no-debugger": true, 58 | "no-duplicate-super": true, 59 | "no-empty": false, 60 | "no-empty-interface": true, 61 | "no-eval": true, 62 | "no-inferrable-types": [ 63 | true, 64 | "ignore-params" 65 | ], 66 | "no-misused-new": true, 67 | "no-non-null-assertion": true, 68 | "no-shadowed-variable": true, 69 | "no-string-literal": false, 70 | "no-string-throw": true, 71 | "no-switch-case-fall-through": true, 72 | "no-trailing-whitespace": true, 73 | "no-unnecessary-initializer": true, 74 | "no-unused-expression": true, 75 | "no-use-before-declare": true, 76 | "no-var-keyword": true, 77 | "object-literal-sort-keys": false, 78 | "one-line": [ 79 | true, 80 | "check-open-brace", 81 | "check-catch", 82 | "check-else", 83 | "check-whitespace" 84 | ], 85 | "prefer-const": true, 86 | "quotemark": [ 87 | true, 88 | "single" 89 | ], 90 | "radix": true, 91 | "semicolon": [ 92 | true, 93 | "always" 94 | ], 95 | "triple-equals": [ 96 | true, 97 | "allow-null-check" 98 | ], 99 | "typedef-whitespace": [ 100 | true, 101 | { 102 | "call-signature": "nospace", 103 | "index-signature": "nospace", 104 | "parameter": "nospace", 105 | "property-declaration": "nospace", 106 | "variable-declaration": "nospace" 107 | } 108 | ], 109 | "unified-signatures": true, 110 | "variable-name": false, 111 | "whitespace": [ 112 | true, 113 | "check-branch", 114 | "check-decl", 115 | "check-operator", 116 | "check-separator", 117 | "check-type" 118 | ], 119 | "no-output-on-prefix": true, 120 | "use-input-property-decorator": true, 121 | "use-output-property-decorator": true, 122 | "use-host-property-decorator": true, 123 | "no-input-rename": true, 124 | "no-output-rename": true, 125 | "use-life-cycle-interface": true, 126 | "use-pipe-transform-interface": true, 127 | "component-class-suffix": true, 128 | "directive-class-suffix": true 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angular-state-management-comparison": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "todo", 11 | "schematics": {}, 12 | "architect": { 13 | "build": { 14 | "builder": "@angular-devkit/build-angular:browser", 15 | "options": { 16 | "outputPath": "dist/angular-state-management-comparison", 17 | "index": "src/index.html", 18 | "main": "src/main.ts", 19 | "polyfills": "src/polyfills.ts", 20 | "tsConfig": "src/tsconfig.app.json", 21 | "assets": [ 22 | "src/favicon.ico", 23 | "src/assets" 24 | ], 25 | "styles": [ 26 | "src/styles.css" 27 | ], 28 | "scripts": [] 29 | }, 30 | "configurations": { 31 | "production": { 32 | "fileReplacements": [ 33 | { 34 | "replace": "src/environments/environment.ts", 35 | "with": "src/environments/environment.prod.ts" 36 | } 37 | ], 38 | "optimization": true, 39 | "outputHashing": "all", 40 | "sourceMap": false, 41 | "extractCss": true, 42 | "namedChunks": false, 43 | "aot": true, 44 | "extractLicenses": true, 45 | "vendorChunk": false, 46 | "buildOptimizer": true 47 | } 48 | } 49 | }, 50 | "serve": { 51 | "builder": "@angular-devkit/build-angular:dev-server", 52 | "options": { 53 | "browserTarget": "angular-state-management-comparison:build" 54 | }, 55 | "configurations": { 56 | "production": { 57 | "browserTarget": "angular-state-management-comparison:build:production" 58 | } 59 | } 60 | }, 61 | "extract-i18n": { 62 | "builder": "@angular-devkit/build-angular:extract-i18n", 63 | "options": { 64 | "browserTarget": "angular-state-management-comparison:build" 65 | } 66 | }, 67 | "test": { 68 | "builder": "@angular-devkit/build-angular:karma", 69 | "options": { 70 | "main": "src/test.ts", 71 | "polyfills": "src/polyfills.ts", 72 | "tsConfig": "src/tsconfig.spec.json", 73 | "karmaConfig": "src/karma.conf.js", 74 | "styles": [ 75 | "src/styles.css" 76 | ], 77 | "scripts": [], 78 | "assets": [ 79 | "src/favicon.ico", 80 | "src/assets" 81 | ] 82 | } 83 | }, 84 | "lint": { 85 | "builder": "@angular-devkit/build-angular:tslint", 86 | "options": { 87 | "tsConfig": [ 88 | "src/tsconfig.app.json", 89 | "src/tsconfig.spec.json" 90 | ], 91 | "exclude": [ 92 | "**/node_modules/**" 93 | ] 94 | } 95 | } 96 | } 97 | }, 98 | "angular-state-management-comparison-e2e": { 99 | "root": "e2e/", 100 | "projectType": "application", 101 | "architect": { 102 | "e2e": { 103 | "builder": "@angular-devkit/build-angular:protractor", 104 | "options": { 105 | "protractorConfig": "e2e/protractor.conf.js", 106 | "devServerTarget": "angular-state-management-comparison:serve" 107 | }, 108 | "configurations": { 109 | "production": { 110 | "devServerTarget": "angular-state-management-comparison:serve:production" 111 | } 112 | } 113 | }, 114 | "lint": { 115 | "builder": "@angular-devkit/build-angular:tslint", 116 | "options": { 117 | "tsConfig": "e2e/tsconfig.e2e.json", 118 | "exclude": [ 119 | "**/node_modules/**" 120 | ] 121 | } 122 | } 123 | } 124 | } 125 | }, 126 | "defaultProject": "angular-state-management-comparison" 127 | } 128 | -------------------------------------------------------------------------------- /src/app/data/fake-backend.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { inject, TestBed } from '@angular/core/testing'; 2 | import { of } from 'rxjs'; 3 | import { catchError, switchMap, tap } from 'rxjs/operators'; 4 | import { convertDateToSeconds } from '../model/date-time-seconds'; 5 | import { createTodoItem } from '../model/todo-item'; 6 | import { FakeBackendService } from './fake-backend.service'; 7 | 8 | 9 | describe('FakeBackendService', () => { 10 | beforeEach(() => { 11 | TestBed.configureTestingModule({ 12 | providers: [FakeBackendService] 13 | }); 14 | }); 15 | 16 | it('should be created', inject([FakeBackendService], (service: FakeBackendService) => { 17 | expect(service).toBeTruthy(); 18 | })); 19 | 20 | it('should get all items', inject([FakeBackendService], (service: FakeBackendService) => { 21 | service.getAll().subscribe(items => { 22 | expect(items.length).toBe(2); 23 | }); 24 | })); 25 | 26 | it('should add one item', inject([FakeBackendService], (service: FakeBackendService) => { 27 | service.addOne(createTodoItem('NEW_ID')) 28 | .pipe( 29 | tap(createdItem => { 30 | expect(createdItem.id).toEqual('NEW_ID'); 31 | }), 32 | switchMap(() => service.getAll()) 33 | ) 34 | .subscribe(items => { 35 | expect(items.length).toBe(3); 36 | expect(items.find(item => item.id === 'NEW_ID')).toBeTruthy(); 37 | }); 38 | })); 39 | 40 | it('should update one item', inject([FakeBackendService], (service: FakeBackendService) => { 41 | service.updateOne('1', createTodoItem('NEW_ID', 'NEW_NAME')) 42 | .pipe( 43 | tap(updatedItem => { 44 | expect(updatedItem.id).toEqual('1'); 45 | expect(updatedItem.name).toEqual('NEW_NAME'); 46 | }), 47 | switchMap(() => service.getAll()) 48 | ) 49 | .subscribe(items => { 50 | expect(items.length).toBe(2); 51 | expect(items.find(item => item.id === 'NEW_ID')).toBeFalsy(); 52 | }); 53 | })); 54 | 55 | it('should throw error when trying to update one item with unknown id', inject([FakeBackendService], (service: FakeBackendService) => { 56 | service.updateOne('3', createTodoItem('NEW_ID', 'NEW_NAME')) 57 | .pipe( 58 | tap(() => { 59 | fail('should throw error'); 60 | }, error => { 61 | expect(error).toEqual('404'); 62 | }), 63 | catchError(() => of(null)), 64 | switchMap(() => service.getAll()) 65 | ) 66 | .subscribe(items => { 67 | expect(items.length).toBe(2); 68 | expect(items.find(item => item.id === 'NEW_ID')).toBeFalsy(); 69 | }); 70 | })); 71 | 72 | it('should toggle createdDateTime to truthy', inject([FakeBackendService], (service: FakeBackendService) => { 73 | service.toggleCompleted('1', convertDateToSeconds(new Date())) 74 | .pipe( 75 | tap(updatedItem => { 76 | expect(updatedItem.id).toEqual('1'); 77 | expect(updatedItem.completedDateTime).toBeTruthy(); 78 | }), 79 | switchMap(() => service.getAll()) 80 | ) 81 | .subscribe(items => { 82 | expect(items.length).toBe(2); 83 | }); 84 | })); 85 | 86 | it('should toggle createdDateTime to falsy', inject([FakeBackendService], (service: FakeBackendService) => { 87 | service.toggleCompleted('2', convertDateToSeconds(new Date())) 88 | .pipe( 89 | tap(updatedItem => { 90 | expect(updatedItem.id).toEqual('2'); 91 | expect(updatedItem.completedDateTime).toBeFalsy(); 92 | }), 93 | switchMap(() => service.getAll()) 94 | ) 95 | .subscribe(items => { 96 | expect(items.length).toBe(2); 97 | }); 98 | })); 99 | 100 | it('should throw error when trying to toggle createdDateTime for item with unknown id', 101 | inject([FakeBackendService], (service: FakeBackendService) => { 102 | service.toggleCompleted('3', convertDateToSeconds(new Date())) 103 | .pipe( 104 | tap(() => { 105 | fail('should throw error'); 106 | }, error => { 107 | expect(error).toEqual('404'); 108 | }), 109 | catchError(() => of(null)), 110 | switchMap(() => service.getAll()) 111 | ) 112 | .subscribe(items => { 113 | expect(items.length).toBe(2); 114 | }); 115 | })); 116 | 117 | it('should remove one item', inject([FakeBackendService], (service: FakeBackendService) => { 118 | service.removeOne('2') 119 | .pipe( 120 | tap(removedItemId => { 121 | expect(removedItemId).toEqual('2'); 122 | }), 123 | switchMap(() => service.getAll()) 124 | ) 125 | .subscribe(items => { 126 | expect(items.length).toBe(1); 127 | expect(items.find(item => item.id === '2')).toBeFalsy(); 128 | }); 129 | })); 130 | 131 | it('should throw error when trying to remove one item with unknown id', inject([FakeBackendService], (service: FakeBackendService) => { 132 | service.removeOne('3') 133 | .pipe( 134 | tap(() => { 135 | fail('should throw error'); 136 | }, error => { 137 | expect(error).toEqual('404'); 138 | }), 139 | catchError(() => of(null)), 140 | switchMap(() => service.getAll()) 141 | ) 142 | .subscribe(items => { 143 | expect(items.length).toBe(2); 144 | }); 145 | })); 146 | }); 147 | 148 | --------------------------------------------------------------------------------