├── .editorconfig ├── .gitignore ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.e2e.json ├── package-lock.json ├── package.json ├── src ├── app │ ├── app.component.html │ ├── app.component.less │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ ├── galaxy.service.spec.ts │ ├── galaxy.service.ts │ ├── star-rater │ │ ├── star-rater.component.html │ │ ├── star-rater.component.less │ │ ├── star-rater.component.spec.ts │ │ └── star-rater.component.ts │ └── typeahead │ │ ├── typeahead.component.html │ │ ├── typeahead.component.less │ │ ├── typeahead.component.spec.ts │ │ └── typeahead.component.ts ├── assets │ ├── .gitkeep │ └── fonts.css ├── browserslist ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── karma.conf.js ├── main.ts ├── polyfills.ts ├── styles.less ├── test.ts ├── tsconfig.app.json ├── tsconfig.spec.json └── tslint.json ├── tsconfig.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://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 | -------------------------------------------------------------------------------- /.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 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events.json 15 | speed-measure-plugin.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.sass-cache 36 | /connect.lock 37 | /coverage 38 | /libpeerconnection.log 39 | npm-debug.log 40 | yarn-error.log 41 | testem.log 42 | /typings 43 | 44 | # System Files 45 | .DS_Store 46 | Thumbs.db 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GalaxyRatingApp 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.6. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 8 | 9 | ## Code scaffolding 10 | 11 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. 12 | 13 | ## Build 14 | 15 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. 16 | 17 | ## Running unit tests 18 | 19 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 20 | 21 | ## Running end-to-end tests 22 | 23 | Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). 24 | 25 | ## Further help 26 | 27 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). 28 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "galaxy-rating-app": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "gr", 11 | "schematics": { 12 | "@schematics/angular:component": { 13 | "style": "less" 14 | } 15 | }, 16 | "architect": { 17 | "build": { 18 | "builder": "@angular-devkit/build-angular:browser", 19 | "options": { 20 | "outputPath": "dist/galaxy-rating-app", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "src/tsconfig.app.json", 25 | "assets": [ 26 | "src/favicon.ico", 27 | "src/assets" 28 | ], 29 | "styles": [ 30 | "./node_modules/bootstrap/dist/css/bootstrap.min.css", 31 | "src/styles.less" 32 | ], 33 | "scripts": [], 34 | "es5BrowserSupport": true 35 | }, 36 | "configurations": { 37 | "production": { 38 | "fileReplacements": [ 39 | { 40 | "replace": "src/environments/environment.ts", 41 | "with": "src/environments/environment.prod.ts" 42 | } 43 | ], 44 | "optimization": true, 45 | "outputHashing": "all", 46 | "sourceMap": false, 47 | "extractCss": true, 48 | "namedChunks": false, 49 | "aot": true, 50 | "extractLicenses": true, 51 | "vendorChunk": false, 52 | "buildOptimizer": true, 53 | "budgets": [ 54 | { 55 | "type": "initial", 56 | "maximumWarning": "2mb", 57 | "maximumError": "5mb" 58 | } 59 | ] 60 | } 61 | } 62 | }, 63 | "serve": { 64 | "builder": "@angular-devkit/build-angular:dev-server", 65 | "options": { 66 | "browserTarget": "galaxy-rating-app:build" 67 | }, 68 | "configurations": { 69 | "production": { 70 | "browserTarget": "galaxy-rating-app:build:production" 71 | } 72 | } 73 | }, 74 | "extract-i18n": { 75 | "builder": "@angular-devkit/build-angular:extract-i18n", 76 | "options": { 77 | "browserTarget": "galaxy-rating-app:build" 78 | } 79 | }, 80 | "test": { 81 | "builder": "@angular-devkit/build-angular:karma", 82 | "options": { 83 | "main": "src/test.ts", 84 | "polyfills": "src/polyfills.ts", 85 | "tsConfig": "src/tsconfig.spec.json", 86 | "karmaConfig": "src/karma.conf.js", 87 | "styles": [ 88 | "./node_modules/bootstrap/dist/css/bootstrap.min.css", 89 | "src/styles.less" 90 | ], 91 | "scripts": [], 92 | "assets": [ 93 | "src/favicon.ico", 94 | "src/assets" 95 | ] 96 | } 97 | }, 98 | "lint": { 99 | "builder": "@angular-devkit/build-angular:tslint", 100 | "options": { 101 | "tsConfig": [ 102 | "src/tsconfig.app.json", 103 | "src/tsconfig.spec.json" 104 | ], 105 | "exclude": [ 106 | "**/node_modules/**" 107 | ] 108 | } 109 | } 110 | } 111 | }, 112 | "galaxy-rating-app-e2e": { 113 | "root": "e2e/", 114 | "projectType": "application", 115 | "prefix": "", 116 | "architect": { 117 | "e2e": { 118 | "builder": "@angular-devkit/build-angular:protractor", 119 | "options": { 120 | "protractorConfig": "e2e/protractor.conf.js", 121 | "devServerTarget": "galaxy-rating-app:serve" 122 | }, 123 | "configurations": { 124 | "production": { 125 | "devServerTarget": "galaxy-rating-app:serve:production" 126 | } 127 | } 128 | }, 129 | "lint": { 130 | "builder": "@angular-devkit/build-angular:tslint", 131 | "options": { 132 | "tsConfig": "e2e/tsconfig.e2e.json", 133 | "exclude": [ 134 | "**/node_modules/**" 135 | ] 136 | } 137 | } 138 | } 139 | } 140 | }, 141 | "defaultProject": "galaxy-rating-app" 142 | } -------------------------------------------------------------------------------- /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 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | import { browser, logging } from 'protractor'; 3 | 4 | describe('workspace-project App', () => { 5 | let page: AppPage; 6 | 7 | beforeEach(() => { 8 | page = new AppPage(); 9 | }); 10 | 11 | it('should display welcome message', () => { 12 | page.navigateTo(); 13 | expect(page.getTitleText()).toEqual('Welcome to galaxy-rating-app!'); 14 | }); 15 | 16 | afterEach(async () => { 17 | // Assert that there are no errors emitted from the browser 18 | const logs = await browser.manage().logs().get(logging.Type.BROWSER); 19 | expect(logs).not.toContain(jasmine.objectContaining({ 20 | level: logging.Level.SEVERE, 21 | } as logging.Entry)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get(browser.baseUrl) as Promise; 6 | } 7 | 8 | getTitleText() { 9 | return element(by.css('gr-root h1')).getText() as Promise; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /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 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "galaxy-rating-app", 3 | "version": "0.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/common": "~7.2.0", 16 | "@angular/compiler": "~7.2.0", 17 | "@angular/core": "~7.2.0", 18 | "@angular/forms": "~7.2.0", 19 | "@angular/platform-browser": "~7.2.0", 20 | "@angular/platform-browser-dynamic": "~7.2.0", 21 | "@angular/router": "~7.2.0", 22 | "bootstrap": "4.2.1", 23 | "core-js": "^2.5.4", 24 | "ngx-bootstrap": "^4.0.1", 25 | "rxjs": "~6.3.3", 26 | "tslib": "^1.9.0", 27 | "zone.js": "~0.8.26" 28 | }, 29 | "devDependencies": { 30 | "@angular-devkit/build-angular": "~0.13.0", 31 | "@angular/cli": "~7.3.6", 32 | "@angular/compiler-cli": "~7.2.0", 33 | "@angular/language-service": "~7.2.0", 34 | "@types/node": "~8.9.4", 35 | "@types/jasmine": "~2.8.8", 36 | "@types/jasminewd2": "~2.0.3", 37 | "codelyzer": "~4.5.0", 38 | "jasmine-core": "~2.99.1", 39 | "jasmine-spec-reporter": "~4.2.1", 40 | "karma": "~4.0.0", 41 | "karma-chrome-launcher": "~2.2.0", 42 | "karma-coverage-istanbul-reporter": "~2.0.1", 43 | "karma-jasmine": "~1.1.2", 44 | "karma-jasmine-html-reporter": "^0.2.2", 45 | "protractor": "~5.4.0", 46 | "ts-node": "~7.0.0", 47 | "tslint": "~5.11.0", 48 | "typescript": "~3.2.2" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 |

Galaxy Rating App

3 |
4 | 8 |
9 |
10 | 14 |
15 |
16 | 20 |
21 |
22 | 23 |
24 |
25 | -------------------------------------------------------------------------------- /src/app/app.component.less: -------------------------------------------------------------------------------- 1 | :host { 2 | form { 3 | padding: 20px; 4 | background-color: #ffffff; 5 | box-shadow: 0px 0px 15px #888888; 6 | margin: 20px auto; 7 | width: 600px; 8 | border-radius: 5px; 9 | min-height: 600px; 10 | .ng-invalid.ng-touched { 11 | border: solid 1px red; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | import { ReactiveFormsModule, FormsModule } from '@angular/forms'; 4 | import { TypeaheadComponent } from './typeahead/typeahead.component'; 5 | import { StarRaterComponent } from './star-rater/star-rater.component'; 6 | import { TypeaheadModule } from 'ngx-bootstrap/typeahead'; 7 | 8 | describe('AppComponent', () => { 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ 12 | AppComponent, 13 | TypeaheadComponent, 14 | StarRaterComponent 15 | ], 16 | imports: [ 17 | ReactiveFormsModule, 18 | FormsModule, 19 | TypeaheadModule.forRoot() 20 | ] 21 | }).compileComponents(); 22 | })); 23 | 24 | it('should create the app', () => { 25 | const fixture = TestBed.createComponent(AppComponent); 26 | const app = fixture.debugElement.componentInstance; 27 | expect(app).toBeTruthy(); 28 | }); 29 | 30 | it(`should have as title 'galaxy-rating-app'`, () => { 31 | const fixture = TestBed.createComponent(AppComponent); 32 | const app = fixture.debugElement.componentInstance; 33 | expect(app.title).toEqual('galaxy-rating-app'); 34 | }); 35 | 36 | it('should render title in a h1 tag', () => { 37 | const fixture = TestBed.createComponent(AppComponent); 38 | fixture.detectChanges(); 39 | const compiled = fixture.debugElement.nativeElement; 40 | expect(compiled.querySelector('h1').textContent).toContain('Galaxy Rating App'); 41 | }); 42 | 43 | it('should enable the star rating formcontrol when galaxy formcontrol has a value', () => { 44 | const fixture = TestBed.createComponent(AppComponent); 45 | fixture.detectChanges(); 46 | const starRatingControl = fixture.componentInstance.galaxyForm.get('rating'); 47 | expect(starRatingControl.disabled).toBe(true); 48 | fixture.componentInstance.galaxyForm.get('galaxy').patchValue(1); 49 | fixture.detectChanges(); 50 | expect(starRatingControl.disabled).toBe(false); 51 | }) 52 | }); 53 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { GalaxyService } from './galaxy.service'; 3 | import { FormGroup, Validators, FormControl } from '@angular/forms'; 4 | 5 | @Component({ 6 | selector: 'gr-root', 7 | templateUrl: './app.component.html', 8 | styleUrls: ['./app.component.less'] 9 | }) 10 | export class AppComponent implements OnInit { 11 | title = 'galaxy-rating-app'; 12 | galaxies = []; 13 | galaxyForm: FormGroup; 14 | 15 | constructor(private galaxyService: GalaxyService) { 16 | } 17 | ngOnInit() { 18 | this.galaxyService.getGalaxies().subscribe((res) => { 19 | this.galaxies = res; 20 | }) 21 | this.createForm(); 22 | this.onFormChanges(); 23 | } 24 | 25 | createForm() { 26 | this.galaxyForm = new FormGroup({ 27 | galaxy: new FormControl({value: null, disabled: false}, [Validators.required]), 28 | rating: new FormControl({value: null, disabled: true}, [Validators.required]), 29 | name: new FormControl({value: null, disabled: false}, [Validators.required]) 30 | }); 31 | } 32 | 33 | onFormChanges() { 34 | this.galaxyForm.get('galaxy').valueChanges.subscribe((val) => { 35 | let ratingControl = this.galaxyForm.get('rating') 36 | if(val) { 37 | ratingControl.enable(); 38 | } 39 | else { 40 | ratingControl.patchValue(null); 41 | ratingControl.disable(); 42 | } 43 | }); 44 | } 45 | 46 | onSubmit() { 47 | console.log(this.galaxyForm.value); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule } from '@angular/core'; 3 | 4 | import { AppComponent } from './app.component'; 5 | import { StarRaterComponent } from './star-rater/star-rater.component'; 6 | import { TypeaheadComponent } from './typeahead/typeahead.component'; 7 | import { TypeaheadModule } from 'ngx-bootstrap/typeahead'; 8 | import { ReactiveFormsModule, FormsModule } from '@angular/forms'; 9 | 10 | @NgModule({ 11 | declarations: [ 12 | AppComponent, 13 | StarRaterComponent, 14 | TypeaheadComponent 15 | ], 16 | imports: [ 17 | BrowserModule, 18 | TypeaheadModule.forRoot(), 19 | ReactiveFormsModule, 20 | FormsModule 21 | ], 22 | providers: [], 23 | bootstrap: [AppComponent] 24 | }) 25 | export class AppModule { } 26 | -------------------------------------------------------------------------------- /src/app/galaxy.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | 3 | import { GalaxyService } from './galaxy.service'; 4 | 5 | describe('GalaxyService', () => { 6 | beforeEach(() => TestBed.configureTestingModule({})); 7 | 8 | it('should be created', () => { 9 | const service: GalaxyService = TestBed.get(GalaxyService); 10 | expect(service).toBeTruthy(); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /src/app/galaxy.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { of } from 'rxjs'; 3 | 4 | @Injectable({ 5 | providedIn: 'root' 6 | }) 7 | export class GalaxyService { 8 | 9 | constructor() { } 10 | 11 | getGalaxies() { 12 | return of([ 13 | { 14 | name: 'Milky Way', 15 | id: 1 16 | }, 17 | { 18 | name: 'LMC', 19 | id: 2 20 | }, 21 | { 22 | name: 'Andromeda', 23 | id: 3 24 | }, 25 | { 26 | name: 'Cigar Galaxy', 27 | id: 4 28 | }, 29 | { 30 | name: 'Pinwheel Galaxy', 31 | id: 5 32 | }, 33 | { 34 | name: 'Sombrero Galaxy', 35 | id: 6 36 | }, 37 | { 38 | name: 'Whirlpool Galaxy', 39 | id: 7 40 | }, 41 | { 42 | name: 'NGC 1300', 43 | id: 8 44 | }, 45 | { 46 | name: 'Tadpole Galaxy', 47 | id: 9 48 | }, 49 | { 50 | name: 'Hoag\'s Object', 51 | id: 10 52 | } 53 | ]) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/app/star-rater/star-rater.component.html: -------------------------------------------------------------------------------- 1 | {{displayText}} 2 |
3 | 4 | 9 | 10 | 11 | 12 |
-------------------------------------------------------------------------------- /src/app/star-rater/star-rater.component.less: -------------------------------------------------------------------------------- 1 | .stars { 2 | cursor: pointer; 3 | 4 | &:hover { 5 | .star polygon { 6 | fill: #ffd055 !important; 7 | } 8 | } 9 | &.disabled:hover { 10 | cursor: not-allowed; 11 | .star { 12 | polygon { 13 | fill: #d8d8d8 !important; 14 | } 15 | } 16 | } 17 | 18 | .star { 19 | float: left; 20 | margin: 0px 5px; 21 | 22 | polygon { 23 | fill: #d8d8d8; 24 | } 25 | 26 | &:hover ~ .star { 27 | polygon { 28 | fill: #d8d8d8 !important; 29 | } 30 | } 31 | &.selected { 32 | polygon { 33 | fill: #ffd055 !important; 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /src/app/star-rater/star-rater.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | import { Component, ViewChild } from '@angular/core'; 3 | 4 | import { StarRaterComponent } from './star-rater.component'; 5 | import { FormControl, ReactiveFormsModule } from '@angular/forms'; 6 | 7 | @Component({ 8 | template: '' 9 | }) 10 | class TestHostComponent { 11 | @ViewChild(StarRaterComponent) 12 | public starRaterComponent: StarRaterComponent; 13 | 14 | public rating: FormControl = new FormControl({value: null, disabled: false}); 15 | } 16 | 17 | describe('StarRaterComponent', () => { 18 | let hostFixture: ComponentFixture; 19 | let testHostComponent: TestHostComponent; 20 | 21 | beforeEach(async(() => { 22 | TestBed.configureTestingModule({ 23 | declarations: [ StarRaterComponent, TestHostComponent ], 24 | imports: [ ReactiveFormsModule ] 25 | }) 26 | .compileComponents(); 27 | })); 28 | 29 | beforeEach(() => { 30 | hostFixture = TestBed.createComponent(TestHostComponent); 31 | testHostComponent = hostFixture.componentInstance; 32 | hostFixture.detectChanges(); 33 | }); 34 | 35 | it('should create', () => { 36 | expect(testHostComponent.starRaterComponent).toBeTruthy(); 37 | }); 38 | 39 | it('should set the star rating based on the formControl value', () => { 40 | testHostComponent.rating.patchValue(3); 41 | hostFixture.detectChanges(); 42 | expect(testHostComponent.starRaterComponent._value).toEqual(3); 43 | const compiled = hostFixture.debugElement.nativeElement; 44 | let selectedStars = compiled.querySelectorAll('gr-star-rater .selected'); 45 | expect(selectedStars.length).toEqual(3) 46 | }); 47 | 48 | it('should set disabled class when formControl is disabled', () => { 49 | testHostComponent.rating.disable(); 50 | hostFixture.detectChanges(); 51 | const compiled = hostFixture.debugElement.nativeElement; 52 | let stars = compiled.querySelector('gr-star-rater .stars'); 53 | expect(stars.classList.contains('disabled')).toBe(true); 54 | }); 55 | 56 | it('should remove disabled class when formControl is enabled', () => { 57 | testHostComponent.rating.disable(); 58 | hostFixture.detectChanges(); 59 | const compiled = hostFixture.debugElement.nativeElement; 60 | let stars = compiled.querySelector('gr-star-rater .stars'); 61 | expect(stars.classList.contains('disabled')).toBe(true); 62 | testHostComponent.rating.enable(); 63 | hostFixture.detectChanges(); 64 | expect(stars.classList.contains('disabled')).toBe(false); 65 | }); 66 | 67 | it('should not call touch or change events when disabled', () => { 68 | testHostComponent.rating.patchValue(3); 69 | testHostComponent.rating.disable(); 70 | hostFixture.detectChanges(); 71 | const compiled = hostFixture.debugElement.nativeElement; 72 | let star1 = compiled.querySelector('gr-star-rater .stars .star'); 73 | star1.dispatchEvent(new Event('click')); 74 | hostFixture.detectChanges(); 75 | expect(testHostComponent.rating.value).toBe(3); 76 | }); 77 | 78 | it('should call touched when star selected', () => { 79 | const compiled = hostFixture.debugElement.nativeElement; 80 | let star1 = compiled.querySelector('gr-star-rater .stars .star'); 81 | star1.dispatchEvent(new Event('click')); 82 | hostFixture.detectChanges(); 83 | expect(compiled.querySelector('gr-star-rater').classList.contains('ng-touched')).toBe(true); 84 | }); 85 | 86 | it('should call change with rating value when star selected', () => { 87 | const compiled = hostFixture.debugElement.nativeElement; 88 | let star3 = compiled.querySelectorAll('gr-star-rater .stars .star')[2]; 89 | star3.dispatchEvent(new Event('click')); 90 | hostFixture.detectChanges(); 91 | expect(testHostComponent.rating.value).toBe(3); 92 | }); 93 | 94 | }); 95 | -------------------------------------------------------------------------------- /src/app/star-rater/star-rater.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, forwardRef } from '@angular/core'; 2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; 3 | 4 | @Component({ 5 | selector: 'gr-star-rater', 6 | templateUrl: './star-rater.component.html', 7 | styleUrls: ['./star-rater.component.less'], 8 | providers: [ 9 | { 10 | provide: NG_VALUE_ACCESSOR, 11 | useExisting: forwardRef(() => StarRaterComponent), 12 | multi: true 13 | } 14 | ] 15 | }) 16 | export class StarRaterComponent implements ControlValueAccessor { 17 | public ratings = [ 18 | { 19 | stars: 1, 20 | text: 'must GTFO ASAP' 21 | }, 22 | { 23 | stars: 2, 24 | text: 'meh' 25 | }, 26 | { 27 | stars: 3, 28 | text: 'it\'s ok' 29 | }, 30 | { 31 | stars: 4, 32 | text: 'I\'d be sad if a black hole ate it' 33 | }, 34 | { 35 | stars: 5, 36 | text: '10/10 would write review on Amazon' 37 | } 38 | ] 39 | public disabled: boolean; 40 | public ratingText: string; 41 | public _value: number; 42 | 43 | onChanged: any = () => {} 44 | onTouched: any = () => {} 45 | 46 | writeValue(val) { 47 | this._value = val; 48 | } 49 | 50 | registerOnChange(fn: any){ 51 | this.onChanged = fn 52 | } 53 | registerOnTouched(fn: any){ 54 | this.onTouched = fn 55 | } 56 | 57 | setDisabledState(isDisabled: boolean): void { 58 | this.disabled = isDisabled; 59 | } 60 | 61 | setRating(star: any) { 62 | if(!this.disabled) { 63 | this._value = star.stars; 64 | this.ratingText = star.text 65 | this.onChanged(star.stars); 66 | this.onTouched(); 67 | } 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/app/typeahead/typeahead.component.html: -------------------------------------------------------------------------------- 1 | 2 | {{model.name}} 3 | 4 | -------------------------------------------------------------------------------- /src/app/typeahead/typeahead.component.less: -------------------------------------------------------------------------------- 1 | :host { 2 | display: block; 3 | } -------------------------------------------------------------------------------- /src/app/typeahead/typeahead.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | import { Component, ViewChild } from '@angular/core'; 3 | 4 | import { TypeaheadComponent } from './typeahead.component'; 5 | import { TypeaheadModule } from 'ngx-bootstrap/typeahead'; 6 | import { FormsModule, FormControl, ReactiveFormsModule, Validators } from '@angular/forms'; 7 | 8 | @Component({ 9 | template: '' 10 | }) 11 | class TestHostComponent { 12 | @ViewChild(TypeaheadComponent) 13 | public typeaheadComponent: TypeaheadComponent; 14 | 15 | public testData = [{id: 1, name: 'foo'},{id: 2, name: 'bar'}]; 16 | public galaxy: FormControl = new FormControl({value: null, disabled: false}); 17 | } 18 | 19 | describe('TypeaheadComponent', () => { 20 | let hostFixture: ComponentFixture; 21 | let testHostComponent: TestHostComponent; 22 | 23 | beforeEach(async(() => { 24 | TestBed.configureTestingModule({ 25 | declarations: [ TypeaheadComponent, TestHostComponent ], 26 | imports: [ 27 | TypeaheadModule.forRoot(), 28 | FormsModule, 29 | ReactiveFormsModule 30 | ] 31 | }) 32 | .compileComponents(); 33 | })); 34 | 35 | beforeEach(() => { 36 | hostFixture = TestBed.createComponent(TestHostComponent); 37 | testHostComponent = hostFixture.componentInstance; 38 | hostFixture.detectChanges(); 39 | }); 40 | 41 | it('should create', () => { 42 | expect(testHostComponent.typeaheadComponent).toBeTruthy(); 43 | }); 44 | 45 | it('should select dropdown based on formControl value', () => { 46 | testHostComponent.galaxy.patchValue(1); 47 | hostFixture.detectChanges(); 48 | expect(testHostComponent.typeaheadComponent.selected).toEqual('foo'); 49 | }); 50 | 51 | it('should send a touch event when item is selected', ()=> { 52 | testHostComponent.typeaheadComponent.selected = { 53 | id: 2, 54 | name: 'foo' 55 | } 56 | const compiled = hostFixture.debugElement.nativeElement; 57 | let component = compiled.querySelector('gr-typeahead'); 58 | expect(component.classList.contains('ng-touched')).toBe(false); 59 | component.querySelector('input').dispatchEvent(new Event('blur')); 60 | hostFixture.detectChanges(); 61 | expect(component.classList.contains('ng-touched')).toBe(true); 62 | }); 63 | 64 | it('should send a change event with the new value when item is selected with new value', () => { 65 | testHostComponent.typeaheadComponent.selected = { 66 | id: 2, 67 | name: 'foo' 68 | } 69 | testHostComponent.typeaheadComponent.onSelect({item: {id: 3}}); 70 | hostFixture.detectChanges(); 71 | expect(testHostComponent.galaxy.value).toBe(3); 72 | }); 73 | 74 | it('should send a null value if the dropdown value is cleared out', () => { 75 | testHostComponent.typeaheadComponent.selected = { 76 | id: 2, 77 | name: 'foo' 78 | } 79 | testHostComponent.typeaheadComponent.onSelect({item: {id: 3}}); 80 | expect(testHostComponent.galaxy.value).toBe(3); 81 | testHostComponent.typeaheadComponent.selected = null; 82 | const compiled = hostFixture.debugElement.nativeElement; 83 | compiled.querySelector('gr-typeahead input').dispatchEvent(new Event('blur')); 84 | expect(testHostComponent.galaxy.value).toBe(null); 85 | }); 86 | 87 | it('should set invalid class when field is required, has been touched, and has no value', () => { 88 | testHostComponent.galaxy.setValidators([Validators.required]); 89 | testHostComponent.typeaheadComponent.selected = null; 90 | const compiled = hostFixture.debugElement.nativeElement; 91 | let component = compiled.querySelector('gr-typeahead'); 92 | expect(testHostComponent.galaxy.touched).toBe(false); 93 | expect(component.classList.contains('ng-invalid')).toBe(false); 94 | component.querySelector('input').dispatchEvent(new Event('blur')); 95 | hostFixture.detectChanges(); 96 | expect(testHostComponent.galaxy.touched).toBe(true); 97 | expect(component.classList.contains('ng-invalid')).toBe(true); 98 | }); 99 | 100 | }); 101 | -------------------------------------------------------------------------------- /src/app/typeahead/typeahead.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, Input, forwardRef } from '@angular/core'; 2 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; 3 | 4 | @Component({ 5 | selector: 'gr-typeahead', 6 | templateUrl: './typeahead.component.html', 7 | styleUrls: ['./typeahead.component.less'], 8 | providers: [ 9 | { 10 | provide: NG_VALUE_ACCESSOR, 11 | useExisting: forwardRef(() => TypeaheadComponent), 12 | multi: true 13 | } 14 | ] 15 | }) 16 | export class TypeaheadComponent implements ControlValueAccessor { 17 | public selected; 18 | @Input() data; 19 | 20 | onChanged: any = () => {} 21 | onTouched: any = () => {} 22 | 23 | //gets the value from the formControl 24 | writeValue(val){ 25 | if(val) { 26 | this.selectDropdown(val); 27 | } 28 | } 29 | 30 | registerOnChange(fn: any){ 31 | this.onChanged = fn 32 | } 33 | registerOnTouched(fn: any){ 34 | this.onTouched = fn 35 | } 36 | 37 | selectDropdown(val) { 38 | this.selected = this.data.filter(x => x.id == val)[0].name; 39 | } 40 | 41 | onSelect(ev) { 42 | this.onTouched(); 43 | if(ev.item.id != this.selected.id) { 44 | this.onChanged(ev.item.id); 45 | } 46 | } 47 | 48 | onBlur(ev) { 49 | this.onTouched(); 50 | if(!this.selected) { 51 | this.onChanged(null); 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tehfedaykin/galaxy-rating-app/cccc9ed3e2a0ebab46e4e746ec2e6a2b56c8c878/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/assets/fonts.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css?family=Montserrat'); 2 | @import url('https://fonts.googleapis.com/css?family=Raleway'); 3 | -------------------------------------------------------------------------------- /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/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /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 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tehfedaykin/galaxy-rating-app/cccc9ed3e2a0ebab46e4e746ec2e6a2b56c8c878/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | GalaxyRatingApp 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /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/galaxy-rating-app'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 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 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /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/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 22 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 23 | 24 | /** 25 | * Web Animations `@angular/platform-browser/animations` 26 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 27 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 28 | */ 29 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 30 | 31 | /** 32 | * By default, zone.js will patch all possible macroTask and DomEvents 33 | * user can disable parts of macroTask/DomEvents patch by setting following flags 34 | * because those flags need to be set before `zone.js` being loaded, and webpack 35 | * will put import in the top of bundle, so user need to create a separate file 36 | * in this directory (for example: zone-flags.ts), and put the following flags 37 | * into that file, and then add the following code before importing zone.js. 38 | * import './zone-flags.ts'; 39 | * 40 | * The flags allowed in zone-flags.ts are listed here. 41 | * 42 | * The following flags will work for all browsers. 43 | * 44 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 45 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 46 | * (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 47 | * 48 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 49 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 50 | * 51 | * (window as any).__Zone_enable_cross_context_check = true; 52 | * 53 | */ 54 | 55 | /*************************************************************************************************** 56 | * Zone JS is required by default for Angular itself. 57 | */ 58 | import 'zone.js/dist/zone'; // Included with Angular CLI. 59 | 60 | 61 | /*************************************************************************************************** 62 | * APPLICATION IMPORTS 63 | */ 64 | -------------------------------------------------------------------------------- /src/styles.less: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | @import (inline) "./assets/fonts.css"; 3 | 4 | body, html { 5 | background-color: #cccccc; 6 | font-family: 'Raleway', sans-serif; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /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/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 | ] 11 | } 12 | -------------------------------------------------------------------------------- /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/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "gr", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "gr", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "module": "es2015", 9 | "moduleResolution": "node", 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "importHelpers": true, 13 | "target": "es5", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2018", 19 | "dom" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rulesDirectory": [ 4 | "codelyzer" 5 | ], 6 | "rules": { 7 | "array-type": false, 8 | "arrow-parens": false, 9 | "deprecation": { 10 | "severity": "warn" 11 | }, 12 | "import-blacklist": [ 13 | true, 14 | "rxjs/Rx" 15 | ], 16 | "interface-name": false, 17 | "max-classes-per-file": false, 18 | "max-line-length": [ 19 | true, 20 | 140 21 | ], 22 | "member-access": false, 23 | "member-ordering": [ 24 | true, 25 | { 26 | "order": [ 27 | "static-field", 28 | "instance-field", 29 | "static-method", 30 | "instance-method" 31 | ] 32 | } 33 | ], 34 | "no-consecutive-blank-lines": false, 35 | "no-console": [ 36 | true, 37 | "debug", 38 | "info", 39 | "time", 40 | "timeEnd", 41 | "trace" 42 | ], 43 | "no-empty": false, 44 | "no-inferrable-types": [ 45 | true, 46 | "ignore-params" 47 | ], 48 | "no-non-null-assertion": true, 49 | "no-redundant-jsdoc": true, 50 | "no-switch-case-fall-through": true, 51 | "no-use-before-declare": true, 52 | "no-var-requires": false, 53 | "object-literal-key-quotes": [ 54 | true, 55 | "as-needed" 56 | ], 57 | "object-literal-sort-keys": false, 58 | "ordered-imports": false, 59 | "quotemark": [ 60 | true, 61 | "single" 62 | ], 63 | "trailing-comma": false, 64 | "no-output-on-prefix": true, 65 | "use-input-property-decorator": true, 66 | "use-output-property-decorator": true, 67 | "use-host-property-decorator": true, 68 | "no-input-rename": true, 69 | "no-output-rename": true, 70 | "use-life-cycle-interface": true, 71 | "use-pipe-transform-interface": true, 72 | "component-class-suffix": true, 73 | "directive-class-suffix": true 74 | } 75 | } 76 | --------------------------------------------------------------------------------