Skip to content

Commit

Permalink
Fix tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
jfly committed Dec 21, 2017
1 parent fbe7139 commit 4d8a817
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
18 changes: 7 additions & 11 deletions tnoodle-ui/README.md
Expand Up @@ -8,28 +8,24 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo
- `yarn install` - Install dependencies.
- `yarn start` - Starts a server accessible at http://localhost:3001/.
- `yarn build` - Compiles to `build/` directory.
- `yarn test` - Run tests.

## Connecting to staging WCA server

Set the `staging` url parameter. For example, try going to http://localhost:3001/staging=true

## Connecting to a development WCA server

Create an OAuth application at http://localhost:3000/oauth/applications/new.
- Name: TNoodle
- Redirect URI: urn:ietf:wg:oauth:2.0:oob
http://localhost:2014/oauth/wca
http://localhost:3001/oauth/wca
- Scopes: public manage_competitions

Then, run `yarn start` with the `REACT_APP_WCA_ORIGIN` and `REACT_APP_TNOODLE_APP_ID` variables set.
For example:

```
REACT_APP_WCA_ORIGIN=http://localhost:3000 REACT_APP_TNOODLE_APP_ID=ddb5b6d20a48e01a4ee95a64bbd5151273dd6f6b5c622b13d19519530ba41658 yarn start
REACT_APP_WCA_ORIGIN=http://localhost:3000 REACT_APP_TNOODLE_APP_ID=tnoodle-development-uid yarn start
```

## Running tests

`yarn test`

## TODO

- warning when using outdated version of tnoodle
- copies input? save back to big json?
- refresh token?
3 changes: 1 addition & 2 deletions tnoodle-ui/src/App.js
Expand Up @@ -3,13 +3,12 @@ import { createStore, combineReducers, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import { Route, Redirect } from 'react-router';
import { Route } from 'react-router';
import createHistory from 'history/createBrowserHistory';
import { ConnectedRouter, routerReducer, routerMiddleware } from 'react-router-redux';

import Home from 'Home';
import Layout from 'Layout';
import * as WcaApi from 'WcaApi';
import * as reducers from 'reducers';
import ManageCompetition from 'ManageCompetition';
import SelectCompetition from 'SelectCompetition';
Expand Down
35 changes: 26 additions & 9 deletions tnoodle-ui/src/WcaApi.js
Expand Up @@ -5,12 +5,13 @@ let WCA_ORIGIN = process.env.REACT_APP_WCA_ORIGIN || 'https://www.worldcubeassoc
let TNOODLE_APP_ID = process.env.REACT_APP_TNOODLE_APP_ID || '6145bf3e65fbad4715b049dae2d72a64b8e9a794010abf518fa9364b05a5dd40';

if(isUsingStaging()) {
// Members of the Software Team can configure this here: https://staging.worldcubeassociation.org/oauth/applications/2
// The TNoodle staging application is created when importing the developer database. See:
// https://github.com/thewca/worldcubeassociation.org/blob/master/WcaOnRails/lib/tasks/db.rake
WCA_ORIGIN = "https://staging.worldcubeassociation.org";
TNOODLE_APP_ID = "28e8d62d7eccf6e9bae5ff33288e5c5e2d567bcd0c893e3083e0c165b0ace5c8";
TNOODLE_APP_ID = "tnoodle-development-uid";
}

let wcaAccessToken = getHashParameter('access_token', null);
let wcaAccessToken = getHashParameter('access_token');
if(wcaAccessToken) {
window.location.hash = "";
localStorage['TNoodle.accessToken'] = wcaAccessToken;
Expand All @@ -20,7 +21,7 @@ if(wcaAccessToken) {
}

export function isUsingStaging() {
return new URLSearchParams(window.location.search).has('staging');
return !!getQueryParameter('staging');
}

export function toWcaUrl(path) {
Expand Down Expand Up @@ -61,11 +62,27 @@ export function getUpcomingManageableCompetitions() {
).then(response => response.json());
}

function getHashParameter(name, alt) {
name = name.replace(/[[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\#&]" + name + "=([^&#]*)");
var results = regex.exec(window.location.hash);
return results === null ? alt : decodeURIComponent(results[1].replace(/\+/g, " "));
function getHashParameter(name) {
return parseQueryString(window.location.hash)[name];
}

function getQueryParameter(name) {
return parseQueryString(window.location.search)[name];
}

// Copied from https://stackoverflow.com/a/3855394/1739415
function parseQueryString(query) {
if (!query) {
return {};
}

return (/^[?#]/.test(query) ? query.slice(1) : query)
.split('&')
.reduce((params, param) => {
let [ key, value ] = param.split('=');
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
return params;
}, {});
}

function wcaApiFetch(path, fetchOptions) {
Expand Down
5 changes: 3 additions & 2 deletions tnoodle-ui/src/WcaCompetitionJson.js
Expand Up @@ -133,8 +133,6 @@ export function checkScrambles(wcaCompetitionJson) {
wcaEvent.rounds.forEach(wcaRound => {
let eventId = wcaEvent.id;
let { roundNumber } = parseActivityCode(wcaRound.id);
let requiredScrambleCountPerGroup = formatToScrambleCount(wcaRound.format);
checked.scramblesNeededCount += requiredScrambleCountPerGroup * wcaRound.scrambleGroupCount;

if(!wcaRound.format) {
checked.warnings.push({
Expand All @@ -145,6 +143,9 @@ export function checkScrambles(wcaCompetitionJson) {
return;
}

let requiredScrambleCountPerGroup = formatToScrambleCount(wcaRound.format);
checked.scramblesNeededCount += requiredScrambleCountPerGroup * wcaRound.scrambleGroupCount;

let roundScramblesPerfect = true;
if(wcaRound.groups.length < wcaRound.scrambleGroupCount) {
checked.roundsWithMissingGroups.push({
Expand Down
4 changes: 3 additions & 1 deletion tnoodle-ui/src/WcaCompetitionJson.test.js
Expand Up @@ -72,6 +72,8 @@ it('checkScrambles finds missing scrambles', () => {
let checkedScrambles = checkScrambles(normalizeCompetitionJson(wcaCompetitionJson));

expect(checkedScrambles).toEqual({
currentScrambleCount: 13,
scramblesNeededCount: 30,
finishedRounds: [
{
id: "333-r3",
Expand All @@ -83,7 +85,7 @@ it('checkScrambles finds missing scrambles', () => {
{
id: "333-r2-gA",
scrambleCount: 3,
requiredScrambleCount: 5,
requiredScrambleCountPerGroup: 5,
},
],
roundsWithMissingGroups: [
Expand Down

0 comments on commit 4d8a817

Please sign in to comment.