]> git.openstreetmap.org Git - nominatim-ui.git/blob - test/reverse.spec.js
Rebundle latest version
[nominatim-ui.git] / test / reverse.spec.js
1 import { test, expect } from './shared.js';
2
3 const reverse_only = !!process.env.REVERSE_ONLY;
4
5 test.describe('Reverse Page', () => {
6
7   test.describe('No search', () => {
8     let page;
9
10     test.beforeAll(async ({ browser }) => {
11       page = await browser.newPage();
12       await page.goto('/reverse.html');
13     });
14
15     test.afterAll(async () => {
16       await page.close();
17     });
18
19     test('should allow switching coordinates', async () => {
20       expect(await page.locator('input[name=lat]').inputValue()).toBe('');
21       expect(await page.locator('input[name=lon]').inputValue()).toBe('');
22
23       await page.locator('#switch-coords').click();
24       // no change
25       expect(await page.locator('input[name=lat]').inputValue()).toBe('');
26       expect(await page.locator('input[name=lon]').inputValue()).toBe('');
27
28       await page.locator('input[name=lat]').fill('5');
29       await page.locator('input[name=lon]').fill('10');
30       await page.locator('#switch-coords').click();
31       // switched
32       expect(await page.locator('input[name=lat]').inputValue()).toBe('10');
33       expect(await page.locator('input[name=lon]').inputValue()).toBe('5');
34     });
35   });
36
37   test.describe('With search', () => {
38     let page;
39
40     test.beforeAll(async ({ browser }) => {
41       page = await browser.newPage();
42       await page.goto('/reverse.html');
43       await page.locator('input[name=lat]').fill('27.1750090510034');
44       await page.locator('input[name=lon]').fill('78.04209025');
45       await page.locator('button[type=submit]').click();
46       await page.locator('#searchresults').waitFor();
47     });
48
49     test.afterAll(async () => {
50       await page.close();
51     });
52
53     test('should return single result', async () => {
54       const results_count = await page.locator(
55         '#searchresults .result'
56       ).count();
57       expect(results_count).toBe(1);
58     });
59
60     test('should display a map', async () => {
61       await page.locator('#map').waitFor();
62       await expect(page.locator('#map')).toHaveCount(1);
63     });
64
65     test('should preserve advanced options when searching from a map click',
66       async () => {
67         // Set layer=address, submit form
68         await page.locator('#searchAdvancedOptions summary').click();
69         await page.locator('#option_layer').waitFor();
70         await page.locator('#option_layer').evaluate(
71           (input) => { input.value = ''; }
72         );
73         await page.locator('#option_layer').fill('address');
74         await page.locator('button[type=submit]').click();
75         await page.waitForFunction(() => {
76           return new URLSearchParams(location.search).get('layer') === 'address';
77         });
78
79         const initialUrl = new URL(page.url());
80         const initialLat = initialUrl.searchParams.get('lat');
81
82         // Click on new position
83         await page.locator('#map').click({ position: { x: 50, y: 50 } });
84
85         // Wait until latitude in URL changed
86         await page.waitForFunction(
87           (previousLat) => {
88             return new URL(window.location.href)
89               .searchParams.get('lat') !== previousLat;
90           },
91           initialLat
92         );
93
94         // Confirm the layer=address is still in URL
95         const refreshedUrl = new URL(page.url());
96         expect(refreshedUrl.searchParams.get('layer')).toBe('address');
97       }
98     );
99
100     test('should redirect to details page on clicking details button',
101       async () => {
102         await page.locator('#searchresults .result a').first().click();
103         await page.locator('table#address').waitFor();
104
105         const current_url = new URL(page.url());
106         expect(current_url.pathname).toBe('/details.html');
107       }
108     );
109
110     if (!reverse_only) {
111       test('should clear results when switching to search page', async () => {
112         await page.locator('nav .nav-link[href="search.html"]').click();
113
114         const results_count = await page.locator(
115           '#searchresults .result'
116         ).count();
117         expect(results_count).toBe(0);
118       });
119     }
120   });
121 });