Browse Source

Fix pagination bug; update tests

sbkwgh 8 years ago
parent
commit
0f065a8477
3 changed files with 71 additions and 65 deletions
  1. 4 15
      routes/category.js
  2. 2 1
      routes/thread.js
  3. 65 49
      test/category.js

+ 4 - 15
routes/category.js

@@ -21,17 +21,6 @@ router.get('/', async (req, res) => {
 
 
 router.get('/:category', async (req, res) => {
-	function concatenateThreads(threads) {
-		let processedThreads = []
-		
-		threads.forEach(category => {
-			let jsonCategory = category.toJSON()
-			processedThreads.push(...jsonCategory.Threads)
-		})
-
-		return processedThreads
-	}
-
 	try {
 		let threads, threadsLatestPost, resThreads, user
 		let { from, limit } = pagination.getPaginationProps(req.query, true)
@@ -68,8 +57,8 @@ router.get('/:category', async (req, res) => {
 		}
 
 		if(req.params.category === 'ALL') {
-			threads = await Category.findAll({ include: threadInclude('ASC') })
-			threadsLatestPost = await Category.findAll({ include: threadInclude('DESC') })
+			threads = await Thread.findAll( threadInclude('ASC')[0] )
+			threadsLatestPost = await Thread.findAll( threadInclude('DESC')[0] )
 		} else {
 			threads = await Category.findOne({
 				where: { name: req.params.category },
@@ -88,11 +77,11 @@ router.get('/:category', async (req, res) => {
 			resThreads = {
 				name: 'All',
 				value: 'ALL',
-				Threads: concatenateThreads(threads),
+				Threads: threads,
 				meta: {}
 			}
 
-			threadsLatestPost = { Threads: concatenateThreads(threadsLatestPost) }
+			threadsLatestPost = { Threads: threadsLatestPost }
 		} else {
 			resThreads = threads.toJSON()
 			resThreads.meta = {}

+ 2 - 1
routes/thread.js

@@ -135,7 +135,8 @@ router.post('/', async (req, res) => {
 		}))
 
 		req.app.get('io').emit('new thread', {
-			category: category.name
+			name: category.name,
+			value: category.value
 		})
 
 	} catch (e) {

+ 65 - 49
test/category.js

@@ -3,8 +3,9 @@ process.env.NODE_ENV = 'test'
 let chai = require('chai')
 let server = require('../server')
 let should = chai.should()
+let expect = chai.expect
 
-let { sequelize } = require('../models')
+let { sequelize, Thread } = require('../models')
 
 const Errors = require('../lib/errors.js')
 
@@ -151,80 +152,95 @@ describe('Category', () => {
 	})
 
 	describe('GET /category/:category', () => {
-		before(async () => {
-			let agent = chai.request.agent(server)
 
+		it('should return allow pagination for category ALL', async () => {
+			let agent = chai.request.agent(server)
+			
 			await agent
 				.post('/api/v1/user/adminaccount/login')
 				.set('content-type', 'application/json')
 				.send({ password: 'password' })
 
-			await agent
-				.post('/api/v1/thread')
+		 	await agent
+		 		.post('/api/v1/category')
 				.set('content-type', 'application/json')
-				.send({ name: 'thread', category: 'category' })
+				.send({ name: 'pagination1' })
 
 			await agent
-				.post('/api/v1/thread')
+		 		.post('/api/v1/category')
 				.set('content-type', 'application/json')
-				.send({ name: 'another_thread', category: 'category' })
+				.send({ name: 'pagination2' })
 
-			await agent
-				.post('/api/v1/post')
-				.set('content-type', 'application/json')
-				.send({ content: 'content here', threadId: 1 })
+			for(var i = 0; i < 30; i++) {
+				let category = 'pagination1'
 
-			await agent
-				.post('/api/v1/post')
-				.set('content-type', 'application/json')
-				.send({ content: 'content here 2', threadId: 1 })
+				if(i % 2) category = 'pagination2'
 
-			await agent
-				.post('/api/v1/post')
-				.set('content-type', 'application/json')
-				.send({ content: 'content here 3', threadId: 1 })
+				let thread = await agent
+					.post('/api/v1/thread')
+					.set('content-type', 'application/json')
+					.send({ name: `THREAD ${i}`, category })
 
-			await agent
-				.post('/api/v1/post')
-				.set('content-type', 'application/json')
-				.send({ content: 'content here', threadId: 2 })
+				await agent
+					.post('/api/v1/post')
+					.set('content-type', 'application/json')
+					.send({ content: `POST ${i}`, threadId: thread.body.id })
+			}
+
+			let pageOne = await agent.get('/api/v1/category/ALL')
+			let pageTwo = await agent.get(pageOne.body.meta.nextURL)
+			let pageThree = await agent.get(pageTwo.body.meta.nextURL)
+
+			pageOne.body.Threads.should.have.length(10)
+			pageOne.body.meta.should.have.property('nextThreadsCount', 10)
+			pageOne.body.Threads[0].Posts[0].should.have.property('content', '<p>POST 29</p>\n')
+
+			pageTwo.body.Threads.should.have.length(10)
+			pageTwo.body.meta.should.have.property('nextThreadsCount', 10)
+			pageTwo.body.Threads[0].Posts[0].should.have.property('content', '<p>POST 19</p>\n')
+
+			pageThree.body.Threads.should.have.length(10)
+			pageThree.body.meta.should.have.property('nextThreadsCount', 0)
+			pageThree.body.Threads[0].Posts[0].should.have.property('content', '<p>POST 9</p>\n')
+			pageThree.body.Threads[9].Posts[0].should.have.property('content', '<p>POST 0</p>\n')
+			expect(pageThree.body.meta.nextURL).to.be.null
 
-			await agent
-				.post('/api/v1/post')
-				.set('content-type', 'application/json')
-				.send({ content: 'content here 2', threadId: 2 })
 
-			await agent
-				.post('/api/v1/post')
-				.set('content-type', 'application/json')
-				.send({ content: 'content here 3', threadId: 2 })
 		})
 
 		it('should return all threads in a category', async () => {
-			let res = await chai.request(server)
-				.get('/api/v1/category/category')
+			let agent = chai.request.agent(server)
+
+			await agent
+				.post('/api/v1/user/adminaccount/login')
+				.set('content-type', 'application/json')
+				.send({ password: 'password' })
+
+
+			for(var i = 0; i < 3; i++) {
+				let thread = await agent
+					.post('/api/v1/thread')
+					.set('content-type', 'application/json')
+					.send({ name: 'thread ' + i, category: 'category' })
+
+				await agent
+					.post('/api/v1/post')
+					.set('content-type', 'application/json')
+					.send({ content: 'content here ' + i, threadId: thread.body.id })
+			}
 
-			res.should.be.json
-			res.should.have.status(200)
-			res.body.should.have.property('name', 'category')
-			res.body.Threads.should.have.property('length', 2)
-			res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
-			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.content', '<p>content here</p>\n')
-			res.body.Threads.should.contain.an.item.with.deep.property('Posts.1.content', '<p>content here 3</p>\n')
-			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
 
-		})
-		it('should return all threads in all categories', async () => {
 			let res = await chai.request(server)
-				.get('/api/v1/category/ALL')
+				.get('/api/v1/category/CATEGORY')
 
 			res.should.be.json
 			res.should.have.status(200)
-			res.body.should.have.property('name', 'All')
-			res.body.Threads.should.have.property('length', 2)
+			res.body.should.have.property('name', 'category')
+			res.body.Threads.should.have.property('length', 3)
 			res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
-			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.content', '<p>content here</p>\n')
-			res.body.Threads.should.contain.an.item.with.deep.property('Posts.1.content', '<p>content here 3</p>\n')
+			res.body.Threads[0].Posts[0].should.have.property('content', '<p>content here 2</p>\n')
+			res.body.Threads[1].Posts[0].should.have.property('content', '<p>content here 1</p>\n')
+			res.body.Threads[2].Posts[0].should.have.property('content', '<p>content here 0</p>\n')
 			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
 
 		})