export function* recommendSaga () { const isLogin = yield call(api.getUserId) const promises = [ api.topPlayList('30'), api.newAlbums('30'), api.topArtists('30') ] if (isLogin) { promises.push(api.dailyRecommend('30')) } yield put({ type: 'home/recommend/start' }) try { const [ playlists, albums, artists, songs ] = yield call(Promise.all, promises) if (playlists.code === 200) { yield put({ type: 'playlists/sync/save', payload: changeCoverImgUrl(playlists.playlists), meta: { more: true, offset: 0 } }) } if (albums.code === 200 && artists.code === 200) { yield put({ type: 'home/recommend/save', payload: { albums: sampleSize(albums.albums, 6), artists: sampleSize(artists.artists, 6) } }) } if (songs && songs.code === 200) { yield put({ type: 'personal/daily/save', payload: songs.recommend.slice(0, 6) }) } } catch (error) { yield put(toastAction('error', '网络出现错误..')) } yield put({ type: 'home/recommend/end' }) }
return _.reduce(categories, function(total, category) { const categoryQuestionsIds = _.map(category.questions, 'id'); const unplayedQuestionsIdsInCategory = _.difference(categoryQuestionsIds, playedQuestionsIds); let replayedQuestionsIdsInCategory = []; if (unplayedQuestionsIdsInCategory.length < category.questionsCount) { //we don't have enough unplayed questions, we have to reuse played ones const replayedQuestionsCount = category.questionsCount - unplayedQuestionsIdsInCategory.length; replayedQuestionsIdsInCategory = _.sampleSize(_.intersection(categoryQuestionsIds, playedQuestionsIds), replayedQuestionsCount); } const selectedCategoryQuestionsIds = _.sampleSize(replayedQuestionsIdsInCategory.concat(unplayedQuestionsIdsInCategory), category.questionsCount); const selectedQuestionsInCategory = _.filter(category.questions, question => _.includes(selectedCategoryQuestionsIds, question.id)); total = total.concat(selectedQuestionsInCategory); return total; }, [])
it('should return undefined if station is invalid', () => { _.sampleSize(Object.keys(stationInfo), 10) .map(station => station + 'random-crap') .forEach(invalidStation => expect(StationManager.getStation(invalidStation)).toBe(undefined) ) })
async getKillerLoadout(): Promise<ILoadout> { return { item: await this.getRandomKiller(), offering: null, perks: _.sampleSize( (await this.fetch()).killers.perks, 4 ) }; }
async getRandomKiller(): Promise<TKiller> { const killers = (await this.fetch()).killers; const killer = _.sample( killers.abilities ); return _.extend({}, killer, { addons: _.sampleSize( killers.addons[ killer.name.replace(/The\s/, '') ], 2 ) }); }
async getSurvivorLoadout(): Promise<ILoadout> { return { item: await this.getRandomItem(), offering: null, perks: _.sampleSize( (await this.fetch()).survivors.perks, 4 ) }; }
async getRandomItem(): Promise<TItem> { const survivors = (await this.fetch()).survivors; const item = _.chain(survivors.items).sample().sample().value(); return _.extend({}, item, { addons: _.sampleSize( survivors.addons[ item.type ], 2 ) }); }
it('should call TripManager for trip options, passing "to" and "from" stations, and date', async () => { const [fromStationName, toStationName] = _.sampleSize(Object.keys(stationInfo), 2) await API.Trips.getTripOptions(fromStationName, toStationName, moment().toDate()) expect(TripManager.getTripOptionsFromNJTPage).toHaveBeenCalled() const [fromStation, toStation, date] = (TripManager.getTripOptionsFromNJTPage as jest.Mock<Promise<Trip[]>>).mock.calls[0] expect(fromStation).toEqual(stationInfo[fromStationName]) expect(toStation).toEqual(stationInfo[toStationName]) expect(moment().diff(moment(date))).toBeLessThan(1000) // Verify dates are less than 1s apart (close enough) })
export function getFakeAnswers(count: number) { return _.sampleSize(fakeAnswersArray, count); }
it('should return StationInfo for valid station', () => { _.sampleSize(Object.keys(stationInfo), 10).forEach(station => { expect(StationManager.getStation(station)).toEqual(stationInfo[station]) }) })