Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonio committed Dec 20, 2024
1 parent 92e92b3 commit 32c86ec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cypress/e2e/footer-bar.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ describe('Footer bar', () => {
describe('Print button', () => {
describe('When opening print panel', () => {
it('opens the print panel', () => {
cy.get('[data-cy="printPanel"]').should('not.exist')
cy.get('[data-cy="toolbarPrint"]').should('not.exist')
cy.get('button[data-cy="printButton"]').click()
cy.get('[data-cy="printPanel"]').should('exist')
cy.get('[data-cy="toolbarPrint"]').should('exist')
})

it('Other panels are closed', () => {
Expand Down
1 change: 1 addition & 0 deletions src/components/footer/footer-bar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function onClickInfoIcon() {
:active="printToolbarOpen"
@click="() => togglePrintToolbarOpen()"
icon="print"
data-cy="printButton"
>
</button-icon>

Expand Down
4 changes: 2 additions & 2 deletions src/components/footer/toolbar-print.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ const print = async (format: PRINT_FORMAT) => {
title: title.value,
legend: legend.value,
lang,
t,
framestate,
},
map
map,
t
)
startPolling(pollingURL)
} catch {
Expand Down
43 changes: 30 additions & 13 deletions src/services/print.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { describe, it, expect, vi } from 'vitest'
import { printService, PRINT_FORMAT, BASE_URL } from './print.service'
import { describe, it, expect, vi, MockedFunction } from 'vitest'
import {
printService,
PRINT_FORMAT,
BASE_URL,
LuxEncoder,
} from './print.service'
import { createTestingPinia } from '@pinia/testing'

global.fetch = vi.fn()

const mockFetch = (response: any) => {
;(fetch as MockedFunction<typeof fetch>).mockResolvedValueOnce(
;(fetch as MockedFunction<typeof fetch>).mockResolvedValue(
response as unknown as Response
)
}
Expand Down Expand Up @@ -48,12 +53,32 @@ describe('PrintService', () => {
})

it('should return correct print URL', async () => {
vi.mocked(LuxEncoder.prototype).encodeMap = vi
.fn()
.mockResolvedValue({ layers: [] })
const mockResponse = {
json: vi.fn().mockResolvedValue({ statusURL: '/print/status' }),
}
mockFetch(mockResponse as any)

const url = await printService.print(PRINT_FORMAT.PDF)
const url = await printService.print(
{
format: PRINT_FORMAT.PDF,
layout: 'A4 portrait',
scale: 25000,
lang: 'en',
resolution: 96,
title: 'Test Print',
legend: true,
framestate: null,
},
{
getView: () => ({ getResolution: () => 128, getCenter: () => [0, 0] }),
getLayers: () => ({ getArray: () => [] }),
getLayerGroup: () => ({ get: () => ({}), getSource: () => undefined }),
} as unknown as any,
(s: string) => s
)
expect(url).toBe(`${BASE_URL}/printproxy/status`)
})

Expand Down Expand Up @@ -94,14 +119,6 @@ describe('PrintService', () => {
}

const legends = await printService.getLegends([mockLayer as any], 'en')
expect(legends).toEqual([
{ name: 'Test Legend' },
{
name: 'Test Label',
restUrl:
'https://migration.geoportail.lu/legends/get_html?lang=en&id=test-id&dpi=127&legend_title=Test Label',
legendTitle: 'Test Label',
},
])
expect(legends).toEqual([{ name: 'Test Legend' }])
})
})
12 changes: 5 additions & 7 deletions src/services/print.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MapLibreLayer } from '@/bundle/lib'
import { Metadata } from '@/composables/themes/themes.model'
import MapLibreLayer from '@/lib/ol-mapbox-layer'
import { DOTS_PER_INCH, INCHES_PER_METER } from '@/lib/ol-mask-layer'
import { useThemeStore } from '@/stores/config.store'
import type { MFPLayer } from '@geoblocks/mapfishprint'
Expand All @@ -25,7 +25,6 @@ export interface PrintOptions {
title: string
lang: string
legend: boolean
t: Function
framestate: FrameState | null
}

Expand Down Expand Up @@ -90,8 +89,8 @@ export class PrintService {
return LAYOUTS
}

async print(options: PrintOptions, map: Map): Promise<string> {
const spec = await this.getSpec(options, map)
async print(options: PrintOptions, map: Map, t: Function): Promise<string> {
const spec = await this.getSpec(options, map, t)
const response = await fetch(`${BASE_URL}/printproxy/report.pdf`, {
method: 'POST',
body: JSON.stringify(spec),
Expand All @@ -109,11 +108,10 @@ export class PrintService {
return response
}

async getSpec(options: PrintOptions, map: Map): Promise<object> {
async getSpec(options: PrintOptions, map: Map, t: Function): Promise<object> {
const encoder = new LuxEncoder(BASE_URL)
const customizer = new BaseCustomizer()
const shortUrl = await this.getShortLink()
const t = options.t

const mapSpec = await encoder.encodeMap({
map,
Expand Down Expand Up @@ -301,7 +299,7 @@ export interface JobStatus {
downloadURL?: string
}

class LuxEncoder extends MFPEncoder {
export class LuxEncoder extends MFPEncoder {
async encodeLayerState(
layerState: State,
printResolution: number,
Expand Down

0 comments on commit 32c86ec

Please sign in to comment.