|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { Store } from '@ngrx/store'; |
| 3 | +import { map, take } from 'rxjs/operators'; |
| 4 | +import { AppState } from './app.model'; |
| 5 | +import { IPartyMember } from './models/base-entity'; |
| 6 | +import { EntityAddItemAction } from './models/entity/entity.actions'; |
| 7 | +import { EntityWithEquipment } from './models/entity/entity.model'; |
| 8 | +import { EntityItemTypes } from './models/entity/entity.reducer'; |
| 9 | +import { |
| 10 | + instantiateEntity, |
| 11 | + ITemplateBaseItem, |
| 12 | +} from './models/game-data/game-data.model'; |
| 13 | +import { |
| 14 | + GameStateAddInventoryAction, |
| 15 | + GameStateHurtPartyAction, |
| 16 | +} from './models/game-state/game-state.actions'; |
| 17 | +import { Item } from './models/item'; |
| 18 | +import { |
| 19 | + getGameBoardedShip, |
| 20 | + getGameInventory, |
| 21 | + getGameKey, |
| 22 | + getGameParty, |
| 23 | + getGamePartyGold, |
| 24 | + getGamePartyWithEquipment, |
| 25 | +} from './models/selectors'; |
| 26 | +import { SpritesService } from './models/sprites/sprites.service'; |
| 27 | +import { WindowService } from './services/window'; |
| 28 | + |
| 29 | +/** |
| 30 | + * Testing providers for the app. |
| 31 | + */ |
| 32 | +export const APP_TESTING_PROVIDERS = [ |
| 33 | + { |
| 34 | + // Mock reload() so it doesn't actually reload the page |
| 35 | + provide: WindowService, |
| 36 | + useValue: { |
| 37 | + reload: jasmine.createSpy('reload'), |
| 38 | + }, |
| 39 | + }, |
| 40 | +]; |
| 41 | + |
| 42 | +export function testAppGetBoarded(store: Store<AppState>): boolean { |
| 43 | + let result: boolean = false; |
| 44 | + store |
| 45 | + .select(getGameBoardedShip) |
| 46 | + .pipe(take(1)) |
| 47 | + .subscribe((s) => (result = s)); |
| 48 | + return result; |
| 49 | +} |
| 50 | + |
| 51 | +export function testAppGetKeyData( |
| 52 | + store: Store<AppState>, |
| 53 | + keyName?: string |
| 54 | +): boolean | undefined { |
| 55 | + if (!keyName) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + let result: boolean | undefined = undefined; |
| 59 | + store |
| 60 | + .select(getGameKey(keyName)) |
| 61 | + .pipe(take(1)) |
| 62 | + .subscribe((s) => (result = s)); |
| 63 | + return result; |
| 64 | +} |
| 65 | + |
| 66 | +export function testAppGetParty(store: Store<AppState>): IPartyMember[] { |
| 67 | + let result: IPartyMember[] = []; |
| 68 | + store |
| 69 | + .select(getGameParty) |
| 70 | + .pipe( |
| 71 | + map((f) => f.toJS()), |
| 72 | + take(1) |
| 73 | + ) |
| 74 | + .subscribe((s) => (result = s)); |
| 75 | + return result; |
| 76 | +} |
| 77 | + |
| 78 | +export function testAppGetPartyWithEquipment( |
| 79 | + store: Store<AppState> |
| 80 | +): EntityWithEquipment[] { |
| 81 | + let result: EntityWithEquipment[] = []; |
| 82 | + store |
| 83 | + .select(getGamePartyWithEquipment) |
| 84 | + .pipe( |
| 85 | + map((f) => f.toJS()), |
| 86 | + take(1) |
| 87 | + ) |
| 88 | + .subscribe((s) => (result = s)); |
| 89 | + return result; |
| 90 | +} |
| 91 | + |
| 92 | +export function testAppGetInventory(store: Store<AppState>): EntityItemTypes[] { |
| 93 | + let result: EntityItemTypes[] | undefined; |
| 94 | + store |
| 95 | + .select(getGameInventory) |
| 96 | + .pipe( |
| 97 | + take(1), |
| 98 | + map((f) => f.toJS()) |
| 99 | + ) |
| 100 | + .subscribe((s) => (result = s)); |
| 101 | + return result as EntityItemTypes[]; |
| 102 | +} |
| 103 | + |
| 104 | +export function testAppGetPartyGold(store: Store<AppState>): number { |
| 105 | + let result = 0; |
| 106 | + store |
| 107 | + .select(getGamePartyGold) |
| 108 | + .pipe(take(1)) |
| 109 | + .subscribe((s) => (result = s)); |
| 110 | + return result; |
| 111 | +} |
| 112 | + |
| 113 | +export function testAppAddToInventory<T extends Item>( |
| 114 | + store: Store<AppState>, |
| 115 | + itemId: string, |
| 116 | + from: ITemplateBaseItem[], |
| 117 | + values?: Partial<T> |
| 118 | +): T { |
| 119 | + const itemInstance = instantiateEntity<T>( |
| 120 | + from.find((f) => f.id === itemId), |
| 121 | + values |
| 122 | + ); |
| 123 | + store.dispatch(new EntityAddItemAction(itemInstance)); |
| 124 | + store.dispatch(new GameStateAddInventoryAction(itemInstance)); |
| 125 | + return itemInstance; |
| 126 | +} |
| 127 | + |
| 128 | +export function testAppDamageParty( |
| 129 | + store: Store<AppState>, |
| 130 | + party: EntityWithEquipment[], |
| 131 | + damage: number |
| 132 | +) { |
| 133 | + store.dispatch( |
| 134 | + new GameStateHurtPartyAction({ partyIds: party.map((p) => p.eid), damage }) |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +export async function testAppLoadSprites() { |
| 139 | + const spritesService = TestBed.inject(SpritesService); |
| 140 | + await spritesService.loadSprites('assets/images/index.json').toPromise(); |
| 141 | +} |
0 commit comments