generated from CMU-313/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
e_oghabayen
committed
Oct 11, 2024
1 parent
04de743
commit bbc1870
Showing
1 changed file
with
87 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,106 @@ | ||
'use strict'; | ||
|
||
define('forum/topic/endorse.tests', [ | ||
'tests/lib/eventemitter2', | ||
'tests/lib/chai', | ||
'tests/lib/sinon', | ||
'forum/topic/postTools', | ||
'jquery', | ||
'api', | ||
'alerts', | ||
], (EventEmitter2, chai, sinon, postTools, $, api, alerts) => { | ||
const { assert } = chai; | ||
|
||
describe('Endorse Feature Client-Side Tests', () => { | ||
let $endorseButton; | ||
let apiPutStub; | ||
let apiDeleteStub; | ||
let alertSuccessStub; | ||
let alertErrorStub; | ||
|
||
beforeEach(() => { | ||
// Set up DOM element | ||
$endorseButton = $('<button data-pid="123" component="post/endorse">Endorse</button>'); | ||
$('body').append($endorseButton); | ||
|
||
// Stub API methods | ||
apiPutStub = sinon.stub(api, 'put'); | ||
apiDeleteStub = sinon.stub(api, 'delete'); | ||
alertSuccessStub = sinon.stub(alerts, 'success'); | ||
alertErrorStub = sinon.stub(alerts, 'error'); | ||
|
||
// Initialize postTools | ||
postTools.init(); | ||
}); | ||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
const $ = require('jquery'); | ||
const postTools = require('../../public/src/client/topic/postTools'); | ||
const api = require('../../public/src/modules/api'); | ||
const alerts = require('../../public/src/modules/alerts'); | ||
|
||
const { assert } = chai; | ||
|
||
describe('Endorse Feature Client-Side Tests', () => { | ||
let $endorseButton; | ||
let apiPutStub; | ||
let apiDeleteStub; | ||
let alertSuccessStub; | ||
let alertErrorStub; | ||
|
||
beforeEach(() => { | ||
// Set up DOM element | ||
$endorseButton = $('<button data-pid="123" component="post/endorse">Endorse</button>'); | ||
$('body').append($endorseButton); | ||
|
||
// Stub API methods | ||
apiPutStub = sinon.stub(api, 'put'); | ||
apiDeleteStub = sinon.stub(api, 'delete'); | ||
alertSuccessStub = sinon.stub(alerts, 'success'); | ||
alertErrorStub = sinon.stub(alerts, 'error'); | ||
|
||
// Initialize postTools | ||
postTools.init(); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore stubs and remove elements | ||
apiPutStub.restore(); | ||
apiDeleteStub.restore(); | ||
alertSuccessStub.restore(); | ||
alertErrorStub.restore(); | ||
$endorseButton.remove(); | ||
afterEach(() => { | ||
// Restore stubs and remove elements | ||
apiPutStub.restore(); | ||
apiDeleteStub.restore(); | ||
alertSuccessStub.restore(); | ||
alertErrorStub.restore(); | ||
$endorseButton.remove(); | ||
}); | ||
|
||
it('should trigger endorse action when endorse button is clicked', (done) => { | ||
apiPutStub.callsFake((endpoint, data, callback) => { | ||
assert.strictEqual(endpoint, '/posts/123/endorse'); | ||
callback(null, { endorsed: true, endorsements: 1 }); | ||
}); | ||
|
||
it('should trigger endorse action when endorse button is clicked', (done) => { | ||
apiPutStub.callsFake((endpoint, data, callback) => { | ||
assert.strictEqual(endpoint, '/posts/123/endorse'); | ||
callback(null, { endorsed: true, endorsements: 1 }); | ||
}); | ||
$endorseButton.click(); | ||
|
||
$endorseButton.click(); | ||
setTimeout(() => { | ||
assert(apiPutStub.calledOnce, 'API.put should be called once'); | ||
assert(alertSuccessStub.calledWith('[[topic:post_endorsed]]'), 'Success alert should be shown'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
setTimeout(() => { | ||
assert(apiPutStub.calledOnce, 'API.put should be called once'); | ||
assert(alertSuccessStub.calledWith('[[topic:post_endorsed]]'), 'Success alert should be shown'); | ||
done(); | ||
}, 100); | ||
it('should update UI after endorsing a post', (done) => { | ||
apiPutStub.callsFake((endpoint, data, callback) => { | ||
callback(null, { endorsed: true, endorsements: 1 }); | ||
}); | ||
|
||
it('should update UI after endorsing a post', (done) => { | ||
apiPutStub.callsFake((endpoint, data, callback) => { | ||
callback(null, { endorsed: true, endorsements: 1 }); | ||
}); | ||
$endorseButton.click(); | ||
|
||
$endorseButton.click(); | ||
setTimeout(() => { | ||
assert.strictEqual($endorseButton.text(), 'Endorsed'); | ||
assert.strictEqual($endorseButton.attr('data-endorsed'), 'true'); | ||
assert.strictEqual($endorseButton.find('.endorsement-count').text(), '1'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
setTimeout(() => { | ||
assert.strictEqual($endorseButton.text(), 'Endorsed'); | ||
assert.strictEqual($endorseButton.attr('data-endorsed'), 'true'); | ||
assert.strictEqual($endorseButton.find('.endorsement-count').text(), '1'); | ||
done(); | ||
}, 100); | ||
it('should handle API errors gracefully', (done) => { | ||
apiPutStub.callsFake((endpoint, data, callback) => { | ||
callback(new Error('Test error')); | ||
}); | ||
|
||
it('should handle API errors gracefully', (done) => { | ||
apiPutStub.callsFake((endpoint, data, callback) => { | ||
callback(new Error('Test error')); | ||
}); | ||
$endorseButton.click(); | ||
|
||
$endorseButton.click(); | ||
setTimeout(() => { | ||
assert(alertErrorStub.calledWith('[[error:endorsing_post]]'), 'Error alert should be shown'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
setTimeout(() => { | ||
assert(alertErrorStub.calledWith('[[error:endorsing_post]]'), 'Error alert should be shown'); | ||
done(); | ||
}, 100); | ||
}); | ||
it('should unendorse a post when clicked on an endorsed post', (done) => { | ||
$endorseButton.attr('data-endorsed', 'true').text('Endorsed'); | ||
$endorseButton.find('.endorsement-count').text('1'); | ||
|
||
it('should unendorse a post when clicked on an endorsed post', (done) => { | ||
$endorseButton.attr('data-endorsed', 'true').text('Endorsed'); | ||
$endorseButton.find('.endorsement-count').text('1'); | ||
|
||
apiDeleteStub.callsFake((endpoint, data, callback) => { | ||
assert.strictEqual(endpoint, '/posts/123/endorse'); | ||
callback(null, { endorsed: false, endorsements: 0 }); | ||
}); | ||
|
||
$endorseButton.click(); | ||
|
||
setTimeout(() => { | ||
assert(apiDeleteStub.calledOnce, 'API.delete should be called once'); | ||
assert.strictEqual($endorseButton.text(), 'Endorse'); | ||
assert.strictEqual($endorseButton.attr('data-endorsed'), 'false'); | ||
assert.strictEqual($endorseButton.find('.endorsement-count').text(), '0'); | ||
assert(alertSuccessStub.calledWith('[[topic:post_unendorsed]]'), 'Success alert should be shown'); | ||
done(); | ||
}, 100); | ||
apiDeleteStub.callsFake((endpoint, data, callback) => { | ||
assert.strictEqual(endpoint, '/posts/123/endorse'); | ||
callback(null, { endorsed: false, endorsements: 0 }); | ||
}); | ||
|
||
$endorseButton.click(); | ||
|
||
setTimeout(() => { | ||
assert(apiDeleteStub.calledOnce, 'API.delete should be called once'); | ||
assert.strictEqual($endorseButton.text(), 'Endorse'); | ||
assert.strictEqual($endorseButton.attr('data-endorsed'), 'false'); | ||
assert.strictEqual($endorseButton.find('.endorsement-count').text(), '0'); | ||
assert(alertSuccessStub.calledWith('[[topic:post_unendorsed]]'), 'Success alert should be shown'); | ||
done(); | ||
}, 100); | ||
}); | ||
}); |