diff --git a/deploy/database/schema.game.sql b/deploy/database/schema.game.sql index 13366dc2d..c7a091833 100644 --- a/deploy/database/schema.game.sql +++ b/deploy/database/schema.game.sql @@ -17,7 +17,7 @@ CREATE TABLE game ( last_winner_id SMALLINT UNSIGNED, tournament_id SMALLINT UNSIGNED, tournament_round_number SMALLINT UNSIGNED, - description VARCHAR(255) NOT NULL, + description VARCHAR(305) NOT NULL, chat TEXT, previous_game_id MEDIUMINT UNSIGNED, FOREIGN KEY (previous_game_id) REFERENCES game(id) diff --git a/deploy/database/updates/03071_long_tournament_description.sql b/deploy/database/updates/03071_long_tournament_description.sql new file mode 100644 index 000000000..801a4547e --- /dev/null +++ b/deploy/database/updates/03071_long_tournament_description.sql @@ -0,0 +1 @@ +ALTER TABLE game MODIFY description VARCHAR(305) NOT NULL; diff --git a/src/api/DummyApiResponder.php b/src/api/DummyApiResponder.php index 3d61069c3..dcd5a927e 100644 --- a/src/api/DummyApiResponder.php +++ b/src/api/DummyApiResponder.php @@ -224,6 +224,13 @@ protected function get_interface_response_loadGameData($args) { ); } + protected function get_interface_response_loadTournamentData($args) { + return $this->load_json_data_from_file( + 'loadTournamentData', + $args['tournament'] . '.json' + ); + } + protected function get_interface_response_countPendingGames() { return $this->load_json_data_from_file( 'countPendingGames', diff --git a/src/engine/BMInterfaceTournament.php b/src/engine/BMInterfaceTournament.php index 43e216d14..42e1c06cd 100644 --- a/src/engine/BMInterfaceTournament.php +++ b/src/engine/BMInterfaceTournament.php @@ -503,9 +503,13 @@ protected function generate_new_games(BMTournament $tournament) { array($gameData['buttonId1'], $gameData['buttonId2']) ); - $description = 'Round ' . $gameData['roundNumber']; - if ('' != $tournament->description) { - $description = $tournament->description . ' ' . $description; + $roundDescription = 'Tournament Round ' . $gameData['roundNumber']; + $tournDescription = $tournament->description; + + if ('' == trim($tournDescription)) { + $tournDescription = $roundDescription; + } else { + $tournDescription = $tournDescription . ' • ' . $roundDescription; } $interfaceResponse = $this->game()->create_game_from_button_ids( @@ -513,7 +517,7 @@ protected function generate_new_games(BMTournament $tournament) { array($gameData['buttonId1'], $gameData['buttonId2']), $buttonNames, $tournament->gameMaxWins, - $description, + $tournDescription, NULL, 0, // needs to be non-null, but also a non-player ID TRUE, diff --git a/src/ui/js/Env.js b/src/ui/js/Env.js index 3e39cab94..795bc4ff5 100644 --- a/src/ui/js/Env.js +++ b/src/ui/js/Env.js @@ -524,6 +524,130 @@ Env.applyBbCodeToHtml = function(htmlToParse) { return outputHtml; }; +Env.removeBbCodeFromHtml = function(htmlToParse) { + // This is all rather more complicated than one might expect, but any attempt + // to parse BB code using simple regular expressions rather than tokenization + // is in the same family as parsing HTML with regular expressions, which + // summons Zalgo. + // (See: http://stackoverflow.com/ + // questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) + + var replacements = { + 'b': {}, + 'i': {}, + 'u': {}, + 's': {}, + 'code': {}, + 'spoiler': {}, + 'quote': {}, + 'game': { + 'isAtomic': true, + }, + 'player': { + 'isAtomic': true, + }, + 'button': { + 'isAtomic': true, + }, + 'set': { + 'isAtomic': true, + }, + 'tourn': { + 'isAtomic': true, + }, + 'wiki': { + 'isAtomic': true, + }, + 'issue': { + 'isAtomic': true, + }, + 'forum': {}, + '[': { + 'isAtomic': true, + }, + }; + + var outputHtml = ''; + var tagStack = []; + + // We want to build a pattern that we can use to identify any single + // BB code start tag + var allStartTagsPattern = ''; + $.each(replacements, function(tagName) { + if (allStartTagsPattern !== '') { + allStartTagsPattern += '|'; + } + // Matches, e.g., '[ b ]' or '[game = "123"]' + // The (?:... part means that we want parentheses around the whole + // thing (so we we can OR it together with other ones), but we don't + // want to capture the value of the whole thing as a group + allStartTagsPattern += + '(?:\\[(' + Env.escapeRegexp(tagName) + ')(?:=([^\\]]*?))?])'; + }); + + var tagName; + + while (htmlToParse) { + var currentPattern = allStartTagsPattern; + if (tagStack.length !== 0) { + // The tag that was most recently opened + tagName = tagStack[tagStack.length - 1]; + // Matches '[/i]' et al. + // (so that we can spot the end of the current tag as well) + currentPattern += + '|(?:\\[(/' + Env.escapeRegexp(tagName) + ')])'; + } + // The first group should be non-greedy (hence the ?), and the last one + // should be greedy, so that nested tags work right + // (E.g., in '...blah[/quote] blah [/quote] blah', we want the first .* + // to end at the first [/quote], not the second) + currentPattern = '^(.*?)(?:' + currentPattern + ')(.*)$'; + // case-insensitive, multi-line + var regExp = new RegExp(currentPattern, 'im'); + + var match = htmlToParse.match(regExp); + if (match) { + var stuffBeforeTag = match[1]; + // javascript apparently believes that capture groups that don't + // match anything are just important as those that do. So we need + // to do some acrobatics to find the ones we actually care about. + // (match[0] is the whole matched string; match[1] is the stuff before + // the tag. So we start with match[2].) + tagName = ''; + for (var i = 2; i < match.length; i++) { + tagName = match[i]; + if (tagName) { + break; + } + } + tagName = tagName.toLowerCase(); + var stuffAfterTag = match[match.length - 1]; + + outputHtml += stuffBeforeTag; + if (tagName.substring(0, 1) === '/') { + // If we've found our closing tag, we can finish the current tag and + // pop it off the stack + tagName = tagStack.pop(); + } else { + if (!replacements[tagName].isAtomic) { + // If there's a closing tag coming along later, push this tag + // on the stack so we'll know we're waiting on it + tagStack.push(tagName); + } + } + + htmlToParse = stuffAfterTag; + } else { + // If we don't find any more BB code tags that we're interested in, + // then we must have reached the end + outputHtml += htmlToParse; + htmlToParse = ''; + } + } + + return outputHtml; +}; + Env.escapeRegexp = function(str) { return str.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'); }; diff --git a/src/ui/js/Game.js b/src/ui/js/Game.js index eccb1732c..4c237c5dd 100644 --- a/src/ui/js/Game.js +++ b/src/ui/js/Game.js @@ -1836,7 +1836,7 @@ Game.pageAddGameHeader = function(action_desc) { if (Api.game.description) { Game.page.append($('
', { - 'text': Api.game.description, + 'html': Env.applyBbCodeToHtml(Api.game.description), 'class': 'gameDescDisplay', })); } diff --git a/src/ui/js/Overview.js b/src/ui/js/Overview.js index bfb001737..770ed47cf 100644 --- a/src/ui/js/Overview.js +++ b/src/ui/js/Overview.js @@ -443,9 +443,11 @@ Overview.addScoreCol = function(gameRow, gameInfo) { Overview.addDescCol = function(gameRow, description) { var descText = ''; - if (typeof(description) == 'string') { - descText = description.substring(0, 30) + - ((description.length > 30) ? '...' : ''); + if (typeof(description) === 'string') { + var descriptionNoMarkup = Env.removeBbCodeFromHtml(description); + + descText = descriptionNoMarkup.substring(0, 30) + + ((descriptionNoMarkup.length > 30) ? '...' : ''); } gameRow.append($('', { 'class': 'gameDescDisplay', diff --git a/src/ui/js/Tournament.js b/src/ui/js/Tournament.js index e1d0a7f82..6583062cc 100644 --- a/src/ui/js/Tournament.js +++ b/src/ui/js/Tournament.js @@ -166,13 +166,7 @@ Tournament.pageAddTournamentHeader = function() { // bgcolor = Tournament.color.opponent; // } - if (Api.tournament.description) { - Tournament.page.append($('
', { - 'text': Api.tournament.description, - 'class': 'gameDescDisplay', - })); - } - + Tournament.pageAddTournamentDescription(); Tournament.page.append($('
')); Tournament.pageAddTournamentInfo(); @@ -306,8 +300,23 @@ Tournament.formFollowTournament = function(e) { Tournament.showLoggedInPage); }; +Tournament.pageAddTournamentDescription = function () { + if (Api.tournament.description) { + Tournament.page.append($('
', { + 'id': 'tournament_desc', + 'html': Env.applyBbCodeToHtml(Api.tournament.description), + 'class': 'gameDescDisplay', + })); + } +}; + Tournament.pageAddTournamentInfo = function () { - var infoDiv = $('
'); + var infoDiv = $( + '
', + { + 'id': 'tournament_info', + } + ); Tournament.page.append(infoDiv); var tournamentTypePar = $('

', { @@ -347,9 +356,18 @@ Tournament.pageAddWinnerInfo = function () { var winnerDiv = $('

'); Tournament.page.append(winnerDiv); - var winnerIdx = Api.tournament.remainCountArray.findIndex( - function(x) {return (x > 0);} + var winnerIdx; + var isWinnerFound = Api.tournament.remainCountArray.some( + function(remainCount, idx) { + winnerIdx = idx; + return remainCount > 0; + } ); + + if (!isWinnerFound) { + return; + } + var winnerPar = $('

', { 'class': 'winner_name', 'text': 'Winner: ' + Api.tournament.playerDataArray[winnerIdx].playerName diff --git a/src/ui/js/TournamentOverview.js b/src/ui/js/TournamentOverview.js index eab9a6a90..97902a75e 100644 --- a/src/ui/js/TournamentOverview.js +++ b/src/ui/js/TournamentOverview.js @@ -284,8 +284,10 @@ TournamentOverview.addTypeCol = function(tournamentRow, tournamentInfo) { TournamentOverview.addDescCol = function(tournamentRow, description) { var descText = ''; if (typeof(description) === 'string') { - descText = description.substring(0, 30) + - ((description.length > 30) ? '...' : ''); + var descriptionNoMarkup = Env.removeBbCodeFromHtml(description); + + descText = descriptionNoMarkup.substring(0, 30) + + ((descriptionNoMarkup.length > 30) ? '...' : ''); } tournamentRow.append($('', { 'class': 'tournamentDescDisplay', diff --git a/test/src/api/responderTournamentTest.php b/test/src/api/responderTournamentTest.php index a36ca08a9..99ac8ab92 100644 --- a/test/src/api/responderTournamentTest.php +++ b/test/src/api/responderTournamentTest.php @@ -211,7 +211,7 @@ public function test_interface_tournament() { $gameOneExpData = $this->generate_init_expected_data_array($gameOneId, 'responder004', 'responder005', 1, 'SPECIFY_DICE'); $gameOneExpData['tournamentId'] = $tournamentId; $gameOneExpData['tournamentRoundNumber'] = 1; - $gameOneExpData['description'] = 'Round 1'; + $gameOneExpData['description'] = 'Tournament Round 1'; $gameOneExpData['currentPlayerIdx'] = FALSE; $gameOneExpData['creatorDataArray'] = array('creatorId' => 0, 'creatorName' => ''); $gameOneExpData['gameActionLog'][0]['message'] = 'Game created automatically'; @@ -243,7 +243,7 @@ public function test_interface_tournament() { $gameTwoExpData['gameSkillsInfo'] = $this->get_skill_info(array('Poison')); $gameTwoExpData['tournamentId'] = $tournamentId; $gameTwoExpData['tournamentRoundNumber'] = 1; - $gameTwoExpData['description'] = 'Round 1'; + $gameTwoExpData['description'] = 'Tournament Round 1'; $gameTwoExpData['activePlayerIdx'] = 0; $gameTwoExpData['playerWithInitiativeIdx'] = 0; $gameTwoExpData['creatorDataArray'] = array('creatorId' => 0, 'creatorName' => ''); @@ -363,7 +363,7 @@ public function test_interface_tournament() { $gameThreeExpData['gameSkillsInfo'] = $this->get_skill_info(array('Poison')); $gameThreeExpData['tournamentId'] = $tournamentId; $gameThreeExpData['tournamentRoundNumber'] = 2; - $gameThreeExpData['description'] = 'Round 2'; + $gameThreeExpData['description'] = 'Tournament Round 2'; $gameThreeExpData['activePlayerIdx'] = 1; $gameThreeExpData['playerWithInitiativeIdx'] = 1; $gameThreeExpData['currentPlayerIdx'] = 1; diff --git a/test/src/engine/BMInterfaceTournamentTest.php b/test/src/engine/BMInterfaceTournamentTest.php index 9f1ca4cff..2ebe8754f 100644 --- a/test/src/engine/BMInterfaceTournamentTest.php +++ b/test/src/engine/BMInterfaceTournamentTest.php @@ -18,4 +18,5 @@ public function test_create_tournament( ) { } + } diff --git a/test/src/ui/js/BMTestUtils.js b/test/src/ui/js/BMTestUtils.js index 4420bff69..df109a693 100644 --- a/test/src/ui/js/BMTestUtils.js +++ b/test/src/ui/js/BMTestUtils.js @@ -46,10 +46,13 @@ BMTestUtils.getAllElements = function() { 'Loader': JSON.stringify(Loader, null, " "), 'Login': JSON.stringify(Login, null, " "), 'Newgame': JSON.stringify(Newgame, null, " "), + 'Newtournament': JSON.stringify(Newtournament, null, " "), 'Newuser': JSON.stringify(Newuser, null, " "), 'OpenGames': JSON.stringify(OpenGames, null, " "), 'Overview': JSON.stringify(Overview, null, " "), 'Profile': JSON.stringify(Profile, null, " "), + 'Tournament': JSON.stringify(Tournament, null, " "), + 'TournamentOverview': JSON.stringify(TournamentOverview, null, " "), 'UserPrefs': JSON.stringify(UserPrefs, null, " "), 'Verify': JSON.stringify(Verify, null, " "), }; @@ -132,6 +135,14 @@ BMTestUtils.testGameId = function(gameDesc) { if (gameDesc == 'NOGAME') { return '10000000'; } }; +// For each tournament reported by responderTest which we use in UI +// tests, set a friendly name for tracking purposes. These values +// need to be kept in sync with responderTest in order for anything +// good to happen. +BMTestUtils.testTournamentId = function(tournamentDesc) { + if (tournamentDesc == 'default') { return '1'; } +}; + // We don't currently usually test reading the URL bar contents, because // that's hard to do within QUnit, but rather override those contents // with hardcoded values that we want to test. @@ -143,6 +154,10 @@ BMTestUtils.overrideGetParameterByName = function() { return BMTestUtils.testGameId(BMTestUtils.GameType); } + if (name == 'tournament') { + return BMTestUtils.testTournamentId(BMTestUtils.TournamentType); + } + // always return the userid associated with tester1 in the fake data if (name == 'id') { return '1'; diff --git a/test/src/ui/js/test_Env.js b/test/src/ui/js/test_Env.js index a81d66ed0..2b10fce8d 100644 --- a/test/src/ui/js/test_Env.js +++ b/test/src/ui/js/test_Env.js @@ -272,6 +272,12 @@ test("test_Env.applyBbCodeToHtml", function(assert) { assert.ok(holder.find('.chatItalic').length == 1, '[i] tag should be converted to HTML'); }); +test("test_Env.removeBbCodeFromHtml", function(assert) { + var rawHtml = 'HTML
[i]BB Code[/i]'; + var newHtml = Env.removeBbCodeFromHtml(rawHtml); + assert.equal(newHtml, 'HTML
BB Code', 'Stripped-down HTML should be correct'); +}); + test("test_Env.escapeRegexp", function(assert) { var rawText = 'example.com'; var escapedPattern = Env.escapeRegexp(rawText); @@ -339,7 +345,7 @@ test("test_Env.toggleSpoiler", function(assert) { var spoiler = $('', { 'class': 'chatSpoiler' }); var eventTriggerSpan = {'target': {'tagName': 'span'}}; var eventTriggerAnchor = {'target': {'tagName': 'a'}}; - + Env.toggleSpoiler.call(spoiler, eventTriggerSpan); assert.ok(spoiler.hasClass('chatExposedSpoiler'), 'Spoiler should be styled as revealed'); diff --git a/test/src/ui/js/test_Newtournament.js b/test/src/ui/js/test_Newtournament.js index f7a57815a..b9abe90e1 100644 --- a/test/src/ui/js/test_Newtournament.js +++ b/test/src/ui/js/test_Newtournament.js @@ -26,7 +26,7 @@ module("Newtournament", { delete Api.player; delete Newtournament.page; delete Newtournament.form; -// delete Newtournament.justCreatedGame; + delete Newtournament.justCreatedTournament; Login.pageModule = null; Newtournament.activity = {}; diff --git a/test/src/ui/js/test_Tournament.js b/test/src/ui/js/test_Tournament.js index 76d99f409..fdb4f2c2c 100644 --- a/test/src/ui/js/test_Tournament.js +++ b/test/src/ui/js/test_Tournament.js @@ -153,17 +153,15 @@ test("test_Tournament.getCurrentTournament", function(assert) { }); test("test_Tournament.showStatePage", function(assert) { -// stop(); -// BMTestUtils.GameType = 'frasquito_wiseman_specifydice'; -// Tournament.getCurrentTournament(function() { -// Tournament.showStatePage(); -// var htmlout = Tournament.page.html(); -// assert.ok(htmlout.length > 0, -// "The created page should have nonzero contents"); -// assert.ok(htmlout.match('vacation16.png'), -// "The game UI contains a vacation icon when the API data reports that one player is on vacation"); -// start(); -// }); + stop(); + BMTestUtils.TournamentType = 'default'; + Tournament.getCurrentTournament(function() { + Tournament.showStatePage(); + var htmlout = Tournament.page.html(); + assert.ok(htmlout.length > 0, + "The created page should have nonzero contents"); + start(); + }); }); test("test_Tournament.showTournamentContents", function(assert) { @@ -171,7 +169,26 @@ test("test_Tournament.showTournamentContents", function(assert) { }); test("test_Tournament.pageAddTournamentHeader", function(assert) { - + stop(); + BMTestUtils.TournamentType = 'default'; + Tournament.getCurrentTournament(function() { + Api.tournament.description = 'description'; + Tournament.showStatePage(); + Tournament.pageAddTournamentHeader(); + var htmlout = Tournament.page.html(); + assert.ok(htmlout.length > 0, + "The created page should have nonzero contents"); + + var tournHeader = $('#tournament_id'); + var tournDesc = $('#tournament_desc'); + var tournInfo = $('#tournament_info'); + + assert.ok(tournHeader.is('div'), 'Tournament header should be a div'); + assert.ok(tournDesc.is('div'), 'Tournament description should be a div'); + assert.ok(tournInfo.is('div'), 'Tournament info should be a div'); + + start(); + }); }); test("test_Tournament.pageAddDismissTournamentLink", function(assert) { @@ -198,6 +215,43 @@ test("test_Tournament.formFollowTournament", function(assert) { }); +test("test_Tournament.pageAddTournamentDescription", function(assert) { + stop(); + BMTestUtils.TournamentType = 'default'; + Tournament.getCurrentTournament(function() { + Api.tournament.description = + '[forum=1,6]text[/forum]456789012345678901234567890' + + '[forum=1,6]text[/forum]456789012345678901234567890' + + '[forum=1,6]text[/forum]456789012345678901234567890' + + '[forum=1,6]text[/forum]456789012345678901234567890' + + '[forum=1,6]text[/forum]4567890...'; + Tournament.showStatePage(); + Tournament.pageAddTournamentDescription(); + + var tournDesc = $('#tournament_desc'); + + var convertedDescription = + 'text' + + '456789012345678901234567890' + + 'text' + + '456789012345678901234567890' + + 'text' + + '456789012345678901234567890' + + 'text' + + '456789012345678901234567890' + + 'text' + + '4567890...'; + + assert.equal( + tournDesc.html(), + convertedDescription, + 'Description text should be correct' + ); + + start(); + }); +}); + test("test_Tournament.pageAddTournamentInfo", function(assert) { }); diff --git a/test/src/ui/js/test_TournamentOverview.js b/test/src/ui/js/test_TournamentOverview.js index 980e60bb9..f22b05f4e 100644 --- a/test/src/ui/js/test_TournamentOverview.js +++ b/test/src/ui/js/test_TournamentOverview.js @@ -4,13 +4,10 @@ module("TournamentOverview", { BMTestUtils.setupFakeLogin(); - // Override Env.getParameterByName to set the game - BMTestUtils.overrideGetParameterByName(); - // Create the tournament_page div so functions have something to modify - if (document.getElementById('tournamentoverview_page') == null) { + if (document.getElementById('tournament_overview_page') == null) { $('body').append($('

', {'id': 'env_message', })); - $('body').append($('
', {'id': 'tournamentoverview_page', })); + $('body').append($('
', {'id': 'tournament_overview_page', })); } // // // set colors for use in game, since tests don't always traverse showStatePage() @@ -19,7 +16,7 @@ module("TournamentOverview", { // 'opponent': '#ddffdd', // }; - Login.pageModule = { 'bodyDivId': 'tournamentoverview_page' }; + Login.pageModule = { 'bodyDivId': 'tournament_overview_page' }; }, 'teardown': function(assert) { @@ -30,12 +27,8 @@ module("TournamentOverview", { // Delete all elements we expect this module to create - // Revert cookies - Env.setCookieNoImages(false); - Env.setCookieCompactMode(false); - // JavaScript variables - delete Api.new_tournaments; + delete Api.tournaments; // delete TournamentOverview.tournament; delete TournamentOverview.page; delete TournamentOverview.form; @@ -44,13 +37,10 @@ module("TournamentOverview", { TournamentOverview.activity = {}; // Page elements - $('#tournamentoverview_page').remove(); - - BMTestUtils.restoreGetParameterByName(); + $('#tournament_overview_page').remove(); BMTestUtils.deleteEnvMessage(); BMTestUtils.cleanupFakeLogin(); - BMTestUtils.restoreGetParameterByName(); // Fail if any other elements were added or removed BMTestUtils.TournamentOverviewPost = BMTestUtils.getAllElements(); @@ -65,109 +55,46 @@ test("test_TournamentOverview_is_loaded", function(assert) { assert.ok(TournamentOverview, "The TournamentOverview namespace exists"); }); -//// The purpose of this test is to demonstrate that the flow of -//// TournamentOverview.showLoggedInPage() is correct for a showXPage function, namely -//// that it calls an API getter with a showStatePage function as a -//// callback. -//// -//// Accomplish this by mocking the invoked functions -test("test_TournamentOverview.showLoggedInPage", function(assert) { -//// expect(5); -//// var cached_getCurrentTournament = Tournament.getCurrentTournament; -//// var cached_showStatePage = Tournament.showStatePage; -//// var getCurrentTournamentCalled = false; -//// Tournament.showStatePage = function() { -//// assert.ok(getCurrentTournamentCalled, "Tournament.getCurrentTournament is called before Tournament.showStatePage"); -//// }; -//// Tournament.getCurrentTournament = function(callback) { -//// getCurrentTournamentCalled = true; -//// assert.equal(callback, Tournament.showStatePage, -//// "Tournament.getCurrentTournament is called with Tournament.showStatePage as an argument"); -//// callback(); -//// }; -//// -//// Tournament.showLoggedInPage(); -//// var item = document.getElementById('tournament_page'); -//// assert.equal(item.nodeName, "DIV", -//// "#tournament_page is a div after showLoggedInPage() is called"); -//// Tournament.getCurrentTournament = cached_getCurrentTournament; -//// Tournament.showStatePage = cached_showStatePage; -}); - -//// Use stop()/start() because the AJAX-using operation needs to -//// finish before its results can be tested -//test("test_Tournament.redrawTournamentPageSuccess", function(assert) { -//// $.ajaxSetup({ async: false }); -//// BMTestUtils.GameType = 'frasquito_wiseman_specifydice'; -//// Tournament.redrawTournamentPageSuccess(); -//// var item = document.getElementById('tournament_page'); -//// assert.equal(item.nodeName, "DIV", -//// "#tournament_page is a div after redrawTournamentPageSuccess() is called"); -//// assert.deepEqual(Tournament.activity, {}, -//// "Tournament.activity is cleared by redrawTournamentPageSuccess()"); -//// $.ajaxSetup({ async: true }); -//}); -// -//// Use stop()/start() because the AJAX-using operation needs to -//// finish before its results can be tested -//test("test_Tournament.redrawTournamentPageFailure", function(assert) { -//// $.ajaxSetup({ async: false }); -//// BMTestUtils.GameType = 'frasquito_wiseman_specifydice'; -//// Tournament.activity.chat = "Some chat text"; -//// Tournament.redrawGamePageFailure(); -//// var item = document.getElementById('tournament_page'); -//// assert.equal(item.nodeName, "DIV", -//// "#tournament_page is a div after redrawGamePageFailure() is called"); -//// assert.equal(Tournament.activity.chat, "Some chat text", -//// "Tournament.activity.chat is retained by redrawTournamentPageSuccess()"); -//// $.ajaxSetup({ async: true }); -//}); +// The purpose of this test is to demonstrate that the flow of +// TournamentOverview.showLoggedInPage() is correct for a showXPage function, namely +// that it calls an API getter with a showStatePage function as a +// callback. // -//// N.B. Almost all of these tests should use stop(), set a test -//// game type, and invoke Tournament.getCurrentTournament(), because that's the -//// way to get the dummy responder data which all the other functions -//// need. Then run tests against the function itself, and end with -//// start(). So the typical format will be: -//// -//// test("test_Tournament.someFunction", function(assert) { -//// stop(); -//// BMTestUtils.GameType = ''; -//// Tournament.getCurrentTournament(function() { -//// -//// Tournament.someFunction(); -//// -//// start(); -//// }); -//// }); -// -//test("test_Tournament.getCurrentTournament", function(assert) { -//// stop(); -//// BMTestUtils.GameType = 'frasquito_wiseman_specifydice'; -//// var gameId = BMTestUtils.testGameId(BMTestUtils.GameType); -//// Tournament.getCurrentTournament(function() { -//// assert.equal(Tournament.tournament, gameId, "Set expected game number"); -//// assert.equal(Api.tournament.load_status, 'ok', 'Successfully loaded game data'); -//// assert.equal(Api.tournament.gameId, Tournament.tournament, 'Parsed correct game number from API'); -//// start(); -//// }); -//}); -// -//test("test_Tournament.showStatePage", function(assert) { -//// stop(); -//// BMTestUtils.GameType = 'frasquito_wiseman_specifydice'; -//// Tournament.getCurrentTournament(function() { -//// Tournament.showStatePage(); -//// var htmlout = Tournament.page.html(); -//// assert.ok(htmlout.length > 0, -//// "The created page should have nonzero contents"); -//// assert.ok(htmlout.match('vacation16.png'), -//// "The game UI contains a vacation icon when the API data reports that one player is on vacation"); -//// start(); -//// }); -//}); +// Accomplish this by mocking the invoked functions +test("test_TournamentOverview.showLoggedInPage", function(assert) { + expect(5); + var cached_getOverview = TournamentOverview.getOverview; + var cached_showStatePage = TournamentOverview.showStatePage; + var getOverviewCalled = false; + TournamentOverview.showPage = function() { + assert.ok( + getOverviewCalled, + "TournamentOverview.getOverview is called before TournamentOverview.showStatePage" + ); + }; + TournamentOverview.getOverview = function(callback) { + getOverviewCalled = true; + assert.equal(callback, TournamentOverview.showPage, + "TournamentOverview.getOverview is called with TournamentOverview.showPage as an argument"); + callback(); + }; + + TournamentOverview.showLoggedInPage(); + var item = document.getElementById('tournament_overview_page'); + console.log(document); + assert.equal(item.nodeName, "DIV", + "#tournament_overview_page is a div after showLoggedInPage() is called"); + + TournamentOverview.getOverview = cached_getOverview; + TournamentOverview.showPage = cached_showStatePage; +}); test("test_TournamentOverview.getOverview", function(assert) { - + stop(); + TournamentOverview.getOverview(function() { + assert.ok(Api.tournaments, "tournaments are parsed from server"); + start(); + }); }); test("test_TournamentOverview.showPage", function(assert) {