KawAnime/test/nuxt.test.js

138 lines
3.9 KiB
JavaScript

/**
* Created by Kylart on 21/05/2017.
*/
import test from 'ava'
import Nuxt from 'nuxt'
import axios from 'axios'
import initFile from '../assets/scripts/init/main.js'
import http from 'http'
import colors from 'colors' // eslint-disable-line
import {resolve, join} from 'path'
import {readFile} from 'fs'
import {userInfo} from 'os'
// We keep the nuxt and server instance
// So we can close them at the end of the test
let nuxt = null
let server = null
const uri = 'htp://localhost:4000'
const DIR = join(userInfo().homedir, '.KawAnime')
let kawAnimeFilesPath = {
local: join(DIR, 'locals.json'),
history: join(DIR, 'history.json'),
watchList: join(DIR, 'lists.json'),
config: join(DIR, 'config.json')
}
// Init Nuxt.js and create a server listening on localhost:4000
test.before('Init Nuxt.js', async () => {
/**
* Nuxt config
*/
const rootDir = resolve(__dirname, '..')
let config = {}
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
config.rootDir = rootDir // project folder
config.dev = false // production build
/**
* Server config
*/
nuxt = new Nuxt(config)
const route = initFile.route(nuxt)
server = http.createServer(route)
await nuxt.build()
server.listen(4000)
console.log(`KawAnime's server is at http://localhost:${server.address().port}`.green)
})
test.cb(`KawAnime's local file exists`, t => {
readFile(`${kawAnimeFilesPath.local}`, t.end)
})
test.cb(`KawAnime's history file exists`, t => {
readFile(`${kawAnimeFilesPath.history}`, t.end)
})
test.cb(`KawAnime's config file exists`, t => {
readFile(`${kawAnimeFilesPath.config}`, t.end)
})
test.cb(`KawAnime's lists file exists`, t => {
readFile(`${kawAnimeFilesPath.watchList}`, t.end)
})
/**
* API test calls
*/
test('/seasons.json route exits and returns elements on Spring 2017', async t => {
const { data } = await axios.get(`${uri}/seasons.json?year=2017&season=spring`)
t.true(data.info.TV.length > 1)
t.true(data.info.OVAs.length > 1)
t.true(data.info.Movies.length > 1)
})
test('/seasons.json route exits and returns elements on Fall 2016', async t => {
const { data } = await axios.get(`${uri}/seasons.json?year=2016&season=fall`)
t.true(data.info.TV.length > 1)
t.true(data.info.OVAs.length > 1)
t.true(data.info.Movies.length > 1)
})
test('/seasons.json route exits and returns and log a error message on Fall 201', async t => {
const { status } = await axios.get(`${uri}/seasons.json?year=201&season=fall`)
t.is(status, 204)
})
test('/news.json route exits and returns 200 elements', async t => {
const { data } = await axios.get(`${uri}/news.json`)
t.is(data.length, 200)
})
// Example of testing only generated html
test('Route / exits and renders title', async t => {
let context = {}
const { html } = await nuxt.renderRoute('/', context)
t.true(html.includes('かわニメ'))
})
test('Route / exits and renders loader', async t => {
let context = {}
const { html } = await nuxt.renderRoute('/', context)
t.true(html.includes('少々お待ち下さいね〜'))
})
// Might be temporary only
test('Route /downloader exits and renders magnet switch', async t => {
let context = {}
const { html } = await nuxt.renderRoute('/downloader', context)
t.true(html.includes('Get Magnets'))
})
test('Route /downloader exits and renders download button', async t => {
let context = {}
const { html } = await nuxt.renderRoute('/downloader', context)
t.true(html.includes('Download!'))
})
// Needs fix from nuxt
// test('Route /downloader', async t => {
// const window = await nuxt.renderAndGetWindow()
// let element = window.document.querySelector('div.form-container')
// t.not(element, null)
// t.is(element.className, 'form-container')
// t.is(element.children[0].className, '.row')
// })
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.test.js', () => {
server.close()
nuxt.close()
})