Created
December 13, 2018 19:32
-
-
Save peb7268/43c359f25756599d0ac185b1a48d123b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tests to run | |
* - what does it do if there are no markets attatched to the study | |
* - state.components.prediction_markets works correctly | |
*/ | |
/* tslint:disable:no-unused-variable */ | |
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | |
import { By } from '@angular/platform-browser'; | |
import { DebugElement } from '@angular/core'; | |
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms'; | |
import { Router, ActivatedRoute } from '@angular/router'; | |
import { EventBusService } from '@client/services'; | |
import { RouterTestingModule } from '@angular/router/testing'; | |
import { AppRoutingModule } from 'apps/intengoadmin/src/app/app-routing.module'; | |
import { HttpClient } from '@angular/common/http'; | |
//STATE DEPS | |
import { IdeationOptionsComponent } from './ideation-options.component'; | |
import { StateService } from 'apps/intengoadmin/src/app/shared/services/state.service'; | |
import { MockStateService } from 'apps/intengoadmin/src/app/shared/services/mock.state.service'; | |
import { IAppState, rootReducer } from 'apps/intengoadmin/src/app/shared/store'; | |
import { ProjectInterface } from 'apps/intengoadmin/src/app/shared/store'; | |
import { ProjectService } from 'apps/intengoadmin/src/app/projects/public/project.service'; | |
import { SocketService } from '@client/services/socket.service'; | |
import { createStore, Reducer, Action, Store } from 'redux'; | |
import { NgRedux, select, NgReduxModule } from '@angular-redux/store'; | |
import { NgReduxTestingModule, MockNgRedux } from '@angular-redux/store/testing'; | |
import { Observable } from 'rxjs/Observable'; | |
import { INITIAL_STATE } from 'apps/intengoadmin/src/app/shared/store/initial_state'; | |
/** | |
* Testing Notes | |
* [] - Why do you need a beforeEach async and a beforeEach? | |
* [] - Best way to test code that is in angular lifecycle hooks | |
* | |
*/ | |
//https://github.com/angular-redux/store/blob/a8169bd893078464b1b4a8295f1bcadf50f1f9de/docs/intro-tutorial.md | |
//https://github.com/angular-redux/example-app/blob/master/src/app/animals/animal/component.spec.ts | |
//https://keyholesoftware.com/2017/02/01/test-driven-angular-2-part-2/ | |
//https://github.com/angular-redux/store/blob/a8169bd893078464b1b4a8295f1bcadf50f1f9de/docs/intro-tutorial.md | |
//https://codecraft.tv/courses/angular/dependency-injection-and-providers/providers/ | |
//https://blog.realworldfullstack.io/real-world-angular-part-9-unit-testing-c62ba20b1d93 | |
//https://codecraft.tv/courses/angular/unit-testing/routing/ | |
//Helpers | |
function getIdeationFromComponent(component) { | |
let state = component.stateService.getState(); | |
let items = component.stateService.extractItemsFromPage(state, component.bid, component.pid); | |
let ideation_state = component.stateService.extractIdeationFromItems(items); | |
return ideation_state; | |
} | |
describe('IdeationOptionsComponent', () => { | |
describe('Initial State', () => { | |
let component: IdeationOptionsComponent; | |
let fixture: ComponentFixture<IdeationOptionsComponent>; | |
let afterViewInitSpy; | |
beforeEach( | |
async(() => { | |
MockNgRedux.reset(); | |
TestBed.configureTestingModule({ | |
declarations: [IdeationOptionsComponent], | |
providers: [ | |
EventBusService, | |
{ provide: StateService, useClass: MockStateService }, | |
FormBuilder, | |
ProjectService, | |
SocketService, | |
{ | |
provide: ActivatedRoute, | |
useValue: { | |
snapshot: { | |
params: { | |
project_id: '5a0b4d8817ac8086cf69540a', | |
bid: '6292', | |
pid: '13036', | |
ideation_id: '5968329' | |
} | |
}, | |
params: Observable.of({ | |
project_id: '5a0b4d8817ac8086cf69540a', | |
bid: '6292', | |
pid: '13036', | |
ideation_id: '5968329' | |
}) | |
} | |
} | |
], | |
imports: [NgReduxTestingModule, RouterTestingModule, HttpClient] | |
}).compileComponents(); | |
}) | |
); | |
beforeEach(() => { | |
let state: any = INITIAL_STATE; | |
let ngRedux = MockNgRedux.getInstance(); | |
ngRedux.configureStore(rootReducer, state, [], []); | |
ngRedux.provideStore(state); | |
let store = ngRedux.getState(); | |
// console.log('MockStore: ', store); | |
fixture = TestBed.createComponent(IdeationOptionsComponent); | |
component = fixture.componentInstance; | |
//Make sure some ngAfterViewInit bootstrap code fires up | |
spyOn(component, 'ngAfterViewInit').and.callThrough(); | |
spyOn(component, 'populatePredictionMarkets').and.callThrough(); | |
spyOn(component, 'initIdeationComponentState').and.callThrough(); | |
spyOn(component, 'updateForm').and.callThrough(); | |
fixture.detectChanges(); | |
}); | |
//Describes initial state with markets | |
describe('ngAfterViewInit with markets', () => { | |
it('component is created', () => expect(component).toBeDefined()); | |
it('should not be linked to a prediction market', () => expect(component.linkedToPredictionMarket).toBeFalsy()); | |
it('should have two prediction markets', () => expect(component.prediction_markets.length).toEqual(2)); | |
it('should not be showing any controls', () => { | |
expect(component.showIdeationControls).toBeFalsy(); | |
expect(component.linkedToPredictionMarket).toBeFalsy(); | |
expect(component.toTieToMarket).toBeTruthy(); | |
}); | |
it('should call ngAfterViewInit', () => expect(component.ngAfterViewInit).toHaveBeenCalled()); | |
it('should call populatePreictionMarkets', () => expect(component.populatePredictionMarkets).toHaveBeenCalled()); | |
it('should call initIdeationComponentState', () => | |
expect(component.initIdeationComponentState).toHaveBeenCalled()); | |
it('should call updateForm', () => expect(component.updateForm).toHaveBeenCalled()); | |
it('It should have a suggestion_display_count', () => expect(component.suggestion_display_count).toBe('50')); | |
it('should have a value for prediction_markets', () => { | |
//https://stackoverflow.com/questions/22413009/jasmine-javascript-testing-tobe-vs-toequal | |
expect(component.prediction_markets).toEqual([ | |
{ | |
pid: 54341, | |
bid: 2984, | |
pmid: 5434695 | |
}, | |
{ | |
pid: 73171, | |
bid: 24115, | |
pmid: 9122899 | |
} | |
]); | |
}); | |
}); | |
}); | |
describe('Ideation Only Path', () => { | |
let component: IdeationOptionsComponent; | |
let state, items, ideation_excercie, selectedConcept; | |
let fixture: ComponentFixture<IdeationOptionsComponent>; | |
let afterViewInitSpy; | |
beforeEach( | |
async(() => { | |
MockNgRedux.reset(); | |
TestBed.configureTestingModule({ | |
declarations: [IdeationOptionsComponent], | |
providers: [ | |
EventBusService, | |
{ provide: StateService, useClass: MockStateService }, | |
FormBuilder, | |
ProjectService, | |
SocketService, | |
{ | |
provide: ActivatedRoute, | |
useValue: { | |
snapshot: { | |
params: { | |
project_id: '5a0b4d8817ac8086cf69540a', | |
bid: '6292', | |
pid: '13036', | |
ideation_id: '5968329' | |
} | |
}, | |
params: Observable.of({ | |
project_id: '5a0b4d8817ac8086cf69540a', | |
bid: '6292', | |
pid: '13036', | |
ideation_id: '5968329' | |
}) | |
} | |
} | |
], | |
imports: [NgReduxTestingModule, RouterTestingModule] | |
}).compileComponents(); | |
}) | |
); | |
beforeEach(() => { | |
let state: any = INITIAL_STATE; | |
let ngRedux = MockNgRedux.getInstance(); | |
selectedConcept = { project_id: '5a0b4d8817ac8086cf69540a', name: 'ideation_only' }; | |
ngRedux.configureStore(rootReducer, state, [], []); | |
ngRedux.provideStore(state); | |
fixture = TestBed.createComponent(IdeationOptionsComponent); | |
component = fixture.componentInstance; | |
component.state = component.stateService.getState(); | |
items = component.stateService.extractItemsFromPage(component.state, component.bid, component.pid); | |
ideation_excercie = component.stateService.extractIdeationFromItems(items); | |
component.selectedConcept = selectedConcept; | |
//Hijack some methods so initIdeationComponentState can be called manually with whatever values we want | |
spyOn(component, 'ngAfterViewInit'); | |
fixture.detectChanges(); | |
}); | |
describe('initIdeationComponentState without markets', () => { | |
it('should not be linked to a market when given an ideation excercise without a market attatched', () => { | |
let ideation_stub = Object.assign({}, ideation_excercie); | |
delete ideation_stub.market; | |
component.initIdeationComponentState(ideation_stub); | |
expect(component.linkedToPredictionMarket).toBeFalsy(); | |
}); | |
it('should not be linked to a market when calling linkToPredictionMarket', () => { | |
component.linkToPredictionMarket(false); | |
expect(component.linkedToPredictionMarket).toBeFalsy(); | |
}); | |
it('should have a selectedConcept.name value of ideation_only', () => { | |
let ideation_stub = Object.assign({}, ideation_excercie); | |
delete ideation_stub.market; | |
component.initIdeationComponentState(ideation_stub); | |
expect(component.selectedConcept).toEqual({ | |
project_id: '5a0b4d8817ac8086cf69540a', | |
name: 'ideation_only' | |
}); | |
}); | |
}); | |
}); | |
describe('Optimization Path', () => { | |
let component: IdeationOptionsComponent; | |
let state, items, ideation_excercie, selectedConcept; | |
let fixture: ComponentFixture<IdeationOptionsComponent>; | |
let afterViewInitSpy; | |
beforeEach( | |
async(() => { | |
MockNgRedux.reset(); | |
TestBed.configureTestingModule({ | |
declarations: [IdeationOptionsComponent], | |
providers: [ | |
EventBusService, | |
{ provide: StateService, useClass: MockStateService }, | |
FormBuilder, | |
ProjectService, | |
SocketService, | |
{ | |
provide: ActivatedRoute, | |
useValue: { | |
snapshot: { | |
params: { | |
project_id: '5a0b4d8817ac8086cf69540a', | |
bid: '87260', | |
pid: '45213', | |
ideation_id: '3360180' | |
} | |
}, | |
params: Observable.of({ | |
project_id: '5a0b4d8817ac8086cf69540a', | |
bid: '87260', | |
pid: '45213', | |
ideation_id: '3360180' | |
}) | |
} | |
} | |
], | |
imports: [NgReduxTestingModule, RouterTestingModule] | |
}).compileComponents(); | |
}) | |
); | |
beforeEach(() => { | |
let state: any = INITIAL_STATE; | |
let ngRedux = MockNgRedux.getInstance(); | |
selectedConcept = { project_id: '5a0b4d8817ac8086cf69540a', name: 'ideation_only' }; | |
ngRedux.configureStore(rootReducer, state, [], []); | |
ngRedux.provideStore(state); | |
fixture = TestBed.createComponent(IdeationOptionsComponent); | |
component = fixture.componentInstance; | |
component.state = component.stateService.getState(); | |
items = component.stateService.extractItemsFromPage(component.state, component.bid, component.pid); | |
ideation_excercie = component.stateService.extractIdeationFromItems(items); | |
component.selectedConcept = selectedConcept; | |
//Hijack some methods so initIdeationComponentState can be called manually with whatever values we want | |
spyOn(component, 'ngAfterViewInit'); | |
fixture.detectChanges(); | |
}); | |
describe('initIdeationComponentState with markets', () => { | |
it('should populate prediction markets for study', () => { | |
expect(component.prediction_markets.length).toEqual(0); | |
component.populatePredictionMarkets(component.stateService.getState()); | |
expect(component.prediction_markets.length).toEqual(2); | |
}); | |
it('should only have markets attatched after initialization', () => { | |
expect(component.prediction_markets.length).toEqual(0); | |
expect(component.market['id']).toBeUndefined(); | |
component.initIdeationComponentState(ideation_excercie); | |
expect(component.market['id']).toBeDefined(); | |
}); | |
it('should have a valid value for this.selectedConcept', () => | |
expect(component.selectedConcept).toEqual(selectedConcept)); | |
it('should be linked to a market when given an ideation excercise with a market attatched', () => { | |
component.initIdeationComponentState(ideation_excercie); | |
expect(component.linkedToPredictionMarket).toBeTruthy(); | |
}); | |
it('should persist the optimization path state after initial selection in the modal', () => { | |
let ideation_state: any = getIdeationFromComponent(component); | |
expect(typeof ideation_state.market).toBe('object'); | |
component.initIdeationOnlyComponent('optimization'); | |
ideation_state = getIdeationFromComponent(component); | |
expect(component.market).toEqual({}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment