Skip to content

Commit

Permalink
redirect to last selected site when not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
arianrhodsandlot committed Jul 31, 2019
1 parent 0b0d1f6 commit bb5c1c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
13 changes: 8 additions & 5 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ const workingDir = path.parse(filePath).dir
const cssLink = getCssLink()
const jsLink = getJsLink()

function isValidSite (site: string) {
return _(sites).map('site')
.includes(site)
}

router
.use(defaultBg, express.static(path.join(workingDir, 'assets/images/default.jpg')))
.use(logger('combined'))
.use((req, res, next) => {
const site = req.cookies.site || sites[0].site
const site = isValidSite(req.cookies.site) ? req.cookies.site : sites[0].site
res.cookie('site', site, { expires: veryLateDate })

const bg = req.cookies.bg || defaultBg
Expand Down Expand Up @@ -65,12 +70,10 @@ router
return
}

const isValidSite = _(sites).map('site')
.includes(site)
if (!isValidSite) {
if (!isValidSite(site)) {
delete parsed.search
parsed.query.query = trimmedQuery
parsed.query.site = sites[0].site
parsed.query.site = res.locals.site
const redirectUrl = url.format(parsed)
res.redirect(redirectUrl)
return
Expand Down
1 change: 0 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import http from 'http'
import _ from 'lodash'
import getPort from 'get-port'
import app from '.'

Expand Down
20 changes: 10 additions & 10 deletions test/integration/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { AddressInfo } from 'net'
import test, { ExecutionContext } from 'ava'
import puppeteer from 'puppeteer'
import sites from '../../src/consts/sites'
import server from '../../src/server'

test.serial.before(async (t) => {
// const x = await promisify(exec)('yarn build')
await new Promise((resolve) => {
server.on('listening', () => {
resolve()
})
})

const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
});

const browser = await puppeteer.launch({ headless: false, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
(t as ExecutionContext<{browser: puppeteer.Browser}>).context.browser = browser
})

test.serial('search', async (t) => {
const { browser } = (t as ExecutionContext<{browser: puppeteer.Browser}>).context
const { port } = server.address() as AddressInfo

const page = await browser.newPage()
const { port } = server.address() as AddressInfo
await page.goto(`http://localhost:${port}`, { waitUntil: 'networkidle2' })
await page.type('input', 'a')
await page.waitForSelector('.album-placeholder')
Expand All @@ -47,14 +42,19 @@ test.serial('search', async (t) => {

test.serial('memorize last selected site', async (t) => {
const { browser } = (t as ExecutionContext<{browser: puppeteer.Browser}>).context
const { port } = server.address() as AddressInfo

const page = await browser.newPage()
const { port } = server.address() as AddressInfo
await page.goto(`http://localhost:${port}`, { waitUntil: 'networkidle2' })
await page.type('input', 'a')
await page.waitForSelector('.album-placeholder')
t.is(await page.evaluate('$(".mdc-chip--selected").index()'), 7)

page.goto(`http://localhost:${port}/search?query=a`)
await page.waitForSelector('.album-placeholder')
t.is(await page.evaluate('$(".mdc-chip--selected").index()'), 7)
t.is(page.url(), `http://localhost:${port}/search?query=a&site=${encodeURIComponent(sites[7].site)}`)

await page.close()
})

Expand Down

0 comments on commit bb5c1c0

Please sign in to comment.