Skip to content

Commit

Permalink
fix(runtime): handle ssr false
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 21, 2020
1 parent c083695 commit 27c3445
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ export function createImage (context, { providers, defaultProvider, presets }: C
}

const nuxtState = context.nuxtState || context.ssrContext.nuxt
if (!nuxtState.data || !nuxtState.data.length) {
nuxtState.data = [{}]
}
const data = nuxtState.data[0]

data.images = data.images || {}

let url = providerUrl
Expand Down
36 changes: 36 additions & 0 deletions test/e2e/no-ssr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { setupTest, createPage, url } from '@nuxt/test-utils'

describe('browser', () => {
setupTest({
fixture: 'fixture/base',
configFile: 'nuxt.config.ts',
config: {
ssr: false
},
browser: true
})
let page
const requests = []

test('should render image', async () => {
page = await createPage()
page.route('**', (route) => {
requests.push(route.request().url())
return route.continue()
})
page.goto(url('/'))
const body = await page.innerHTML('body')
expect(body).not.toContain('/_/w_30/2000px-Aconcagua2016.jpg')
const placeholderRequest = requests.find(request => request.match('/_image/local/_/w_30/2000px-Aconcagua2016.jpg'))
expect(placeholderRequest).not.toBeNull()
const positiveRequest = requests.find(request => request.match('2000px-Aconcagua2016.jpg'))
expect(positiveRequest).not.toBeNull()
const negativeRequest = requests.find(request => request.match('1280px-K2_2006b.jpg'))
expect(negativeRequest).toBeUndefined()
})
test('change image location', async () => {
await page.click('#button')
const positiveRequest = requests.find(request => request.match('1280px-K2_2006b.jpg'))
expect(positiveRequest).not.toBeUndefined()
})
})
2 changes: 1 addition & 1 deletion test/e2e/browser.test.ts → test/e2e/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('browser', () => {
})
page.goto(url('/'))
const body = await page.innerHTML('body')
expect(body).toContain('/_image/local/_/w_30/2000px-Aconcagua2016.jpg')
expect(body).toContain('/_/w_30/2000px-Aconcagua2016.jpg')
const positiveRequest = requests.find(request => request.match('2000px-Aconcagua2016.jpg'))
expect(positiveRequest).not.toBeNull()
const negativeRequest = requests.find(request => request.match('1280px-K2_2006b.jpg'))
Expand Down

0 comments on commit 27c3445

Please sign in to comment.