Skip to content

Commit

Permalink
Merge "Replace .done/.fail/.always with .then/.catch in TalkOverlay e…
Browse files Browse the repository at this point in the history
…t al."
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Aug 21, 2018
2 parents f6a1bf4 + 6fba8ce commit d42523e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
6 changes: 3 additions & 3 deletions resources/mobile.talk.overlays/TalkOverlay.js
Expand Up @@ -107,7 +107,9 @@
// clear actual content, if any
this.$( '.topic-title-list' ).empty();

this.pageGateway.getPage( options.title ).fail( function ( resp ) {
this.pageGateway.getPage( options.title ).then( function ( pageData ) {
self._addContent( pageData, options );
}, function ( resp ) {
// If the API returns the error code 'missingtitle', that means the
// talk page doesn't exist yet.
if ( resp === 'missingtitle' ) {
Expand All @@ -127,8 +129,6 @@
window.location = mw.util.getUrl( options.title );
}
}
} ).done( function ( pageData ) {
self._addContent( pageData, options );
} );
},

Expand Down
4 changes: 2 additions & 2 deletions resources/mobile.talk.overlays/TalkSectionAddOverlay.js
Expand Up @@ -130,7 +130,7 @@
isOnTalkPage = self.title === self.currentPageTitle;

this.showHidden( '.saving-header' );
this.save().done( function ( status ) {
this.save().then( function ( status ) {
if ( status === 'ok' ) {
if ( isOnTalkPage ) {
M.emit( 'talk-added-wo-overlay' );
Expand All @@ -141,7 +141,7 @@
self.hide();
}
}
} ).fail( function ( error ) {
}, function ( error ) {
var editMsg = mw.msg( 'mobile-frontend-talk-topic-error' );

self.$confirm.prop( 'disabled', false );
Expand Down
15 changes: 10 additions & 5 deletions resources/mobile.talk.overlays/TalkSectionOverlay.js
Expand Up @@ -95,7 +95,7 @@
renderFromApi: function ( options ) {
var self = this;

this.pageGateway.getPage( options.title ).done( function ( pageData ) {
this.pageGateway.getPage( options.title ).then( function ( pageData ) {
var page = new Page( pageData );
options.section = page.getSection( options.id );
self.render( options );
Expand All @@ -119,6 +119,9 @@
var val = this.$textarea.val(),
self = this;

function enableSaveButton() {
self.$saveButton.prop( 'disabled', false );
}
if ( val ) {
// show a spinner
this.showSpinner();
Expand All @@ -132,13 +135,15 @@
section: this.options.id,
appendtext: val,
redirect: true
} ).done( function () {
} ).then( function () {
popup.show( mw.msg( 'mobile-frontend-talk-reply-success' ) );
// invalidate the cache
self.pageGateway.invalidatePage( self.options.title );

self.renderFromApi( self.options );
} ).fail( function ( data, response ) {

enableSaveButton();
}, function ( data, response ) {
// FIXME: Code sharing with EditorOverlay?
var msg,
// When save failed with one of these error codes, the returned
Expand All @@ -161,8 +166,8 @@

self.clearSpinner();
popup.show( msg, 'toast error' );
} ).always( function () {
self.$saveButton.prop( 'disabled', false );

enableSaveButton();
} );
} else {
this.$textarea.addClass( 'error' );
Expand Down
76 changes: 45 additions & 31 deletions tests/qunit/mobile.talk.overlays/test_TalkOverlay.js
Expand Up @@ -7,20 +7,22 @@
QUnit.module( 'MobileFrontend TalkOverlay', {
setup: function () {
this.api = new mw.Api();
this.getPageDeferredReject = $.Deferred().reject( 'missingtitle' );
this.getPageDeferredResolve = $.Deferred().resolve( {
title: 'Talk:Topic',
id: 1,
lead: '',
sections: [
{
id: 50,
line: 'Topic 1'
}
]
} );
this.sandbox.stub( PageGateway.prototype, 'getPage' ).withArgs( 'Talk:No exist' ).returns(
$.Deferred().reject( 'missingtitle' )
this.getPageDeferredReject
).withArgs( 'Talk:Topic' ).returns(
$.Deferred().resolve( {
title: 'Talk:Topic',
id: 1,
lead: '',
sections: [
{
id: 50,
line: 'Topic 1'
}
]
} )
this.getPageDeferredResolve
);

this.user = user.getName() || '';
Expand All @@ -36,18 +38,26 @@
title: 'Talk:No exist'
},
overlay = new TalkOverlay( options ),
page = overlay.page;
page,
self = this;

mw.config.set( 'wgUserName', null );
assert.strictEqual( page.title, 'Talk:No exist', 'Title set' );
assert.strictEqual( page.getSections().length, 0, 'A page was setup with no sections' );

// reload discussion board via ajax
overlay._loadContent( options );
assert.strictEqual( page.getSections().length, 0, 'Discussions reloaded, still no sections' );
return this.getPageDeferredReject.catch( function () {
page = overlay.page;
assert.strictEqual( page.title, 'Talk:No exist', 'Title set' );
assert.strictEqual( page.getSections().length, 0, 'A page was setup with no sections' );

// reload discussion board via ajax
overlay._loadContent( options );

return self.getPageDeferredReject.catch( function () {
assert.strictEqual( page.getSections().length, 0, 'Discussions reloaded, still no sections' );

// check whether there is an Add discussion button
assert.strictEqual( overlay.$( '.add' ).length, 0, 'There is no "Add discussion" button' );
// check whether there is an Add discussion button
assert.strictEqual( overlay.$( '.add' ).length, 0, 'There is no "Add discussion" button' );
} );
} );
} );

QUnit.test( '#TalkOverlay (logged in)', function ( assert ) {
Expand All @@ -60,9 +70,11 @@
} );

assert.ok( overlay.$( '.add' ).length > 0, 'There is an "Add discussion" button' );
assert.strictEqual( overlay.$( '.content-header' ).text().trim(),
mw.msg( 'mobile-frontend-talk-explained-empty' ),
'Check the header knows it is empty.' );
return this.getPageDeferredReject.catch( function () {
assert.strictEqual( overlay.$( '.content-header' ).text().trim(),
mw.msg( 'mobile-frontend-talk-explained-empty' ),
'Check the header knows it is empty.' );
} );
} );

QUnit.test( '#TalkOverlay (existing page lists section headings)', function ( assert ) {
Expand All @@ -71,14 +83,16 @@
title: 'Talk:Topic'
} );

assert.ok( overlay.$( '.topic-title-list li' ).length === 1, 'One topic heading is listed' );
assert.strictEqual( overlay.$( '.topic-title-list li a' ).eq( 0 ).text(), 'Topic 1',
'The text of the second item is the section heading.' );
assert.strictEqual( overlay.$( '.topic-title-list li a' ).data( 'id' ), 50,
'The data id is set.' );
assert.strictEqual( overlay.$( '.content-header' ).text().trim(),
mw.msg( 'mobile-frontend-talk-explained' ),
'Check the header knows it is not empty.' );
return this.getPageDeferredResolve.then( function () {
assert.ok( overlay.$( '.topic-title-list li' ).length === 1, 'One topic heading is listed' );
assert.strictEqual( overlay.$( '.topic-title-list li a' ).eq( 0 ).text(), 'Topic 1',
'The text of the second item is the section heading.' );
assert.strictEqual( overlay.$( '.topic-title-list li a' ).data( 'id' ), 50,
'The data id is set.' );
assert.strictEqual( overlay.$( '.content-header' ).text().trim(),
mw.msg( 'mobile-frontend-talk-explained' ),
'Check the header knows it is not empty.' );
} );
} );

}( mw.mobileFrontend, jQuery ) );
Expand Up @@ -21,13 +21,11 @@
assert.strictEqual( overlay.$( 'input' ).val(), 'Testtitle', 'Testtitle set' );
assert.strictEqual( overlay.$( 'textarea' ).val(), 'Testcontent', 'Testcontent set' );
// Test the save of the new dicsussion
overlay.save().done( function ( status ) {
return overlay.save().then( function ( status ) {
assert.strictEqual( status, 'ok', 'The new discussion was saved' );
} ).fail( function ( error ) {
assert.strictEqual( error, 'ok', 'The new discussion was saved' );
// check, if the save was recognized (so the overlay can hide without confirmation of the user)
assert.strictEqual( overlay._saveHit, true, 'The save was recognized' );
} );
// check, if the save was recognized (so the overlay can hide without confirmation of the user)
assert.strictEqual( overlay._saveHit, true, 'The save was recognized' );
} );

}( mw.mobileFrontend, jQuery ) );

0 comments on commit d42523e

Please sign in to comment.