<commit_before>import search, { fetchRelatedForUser } from './map-search';

import * as address from '../../../shared/services/adressen/adressen-nummeraanduiding';
// import * as monument from '../../../shared/services/monument/monument';
import * as vestiging from '../../../shared/services/vestiging/vestiging';

import { getByUrl } from '../../../shared/services/api/api';

jest.mock('../../../shared/services/api/api');
jest.mock('../../../shared/services/adressen/adressen-nummeraanduiding');
jest.mock('../../../shared/services/monument/monument');
jest.mock('../../../shared/services/vestiging/vestiging');

describe('mapSearch service', () => {
  let user;

  beforeEach(() => {
    user = {
      authenticated: true,
      scopes: [
        'HR/R'
      ],
      name: '<EMAIL>'
    };
  });

  describe('search action', () => {
    it('should return results from search for bommen', async () => {
      getByUrl.mockReturnValue(Promise.resolve({
        features: [
          {
            properties: {
              type: 'bommenkaart/verdachtgebied' // not in map-search
            }
          }
        ]
      }));

      const data = await search({ latitude: 1, longitude: 0 }, {
        authenticated: false,
        accessToken: '',
        scopes: [],
        name: '',
        error: false
      });

      expect(data.results).toEqual([{
        categoryLabel: 'Explosief',
        results: Array(9).fill({ // !!!!IMPORTANT: UPDATE WITH +1 WHEN ADDING NEW GEOSEARCH
          categoryLabel: 'Explosief',
          label: undefined,
          parent: undefined,
          statusLabel: 'verdacht gebied',
          type: 'bommenkaart/verdachtgebied',
          uri: undefined
        }),
        subCategories: [],
        type: 'bommenkaart/verdachtgebied'
      }]);
    });

    it('should return results and ignore the ignore the failing calls (http 500 errors)', async () => {
      getByUrl.mockImplementation((url) => {
        if (url.indexOf('/nap/') > 0) {
          return Promise.reject();
        }
        return (
          Promise.resolve({
            features: [
              {
                properties: {
                  type: 'bommenkaart/verdachtgebied' // not in map-search
                }
              }
            ]
          })
        );
      });

      const data = await search({ latitude: 1, longitude: 0 }, {
        authenticated: false,
        accessToken: '',
        scopes: [],
        name: '',
        error: false
      });

      expect(data.results).toEqual([{
        categoryLabel: 'Explosief',
        results: Array(8).fill({ // !!!!IMPORTANT: UPDATE WITH +1 WHEN ADDING NEW GEOSEARCH
          categoryLabel: 'Explosief',
          label: undefined,
          parent: undefined,
          statusLabel: 'verdacht gebied',
          type: 'bommenkaart/verdachtgebied',
          uri: undefined
        }),
        subCategories: [],
        type: 'bommenkaart/verdachtgebied'
      }]);
    });
  });

  describe('fetchRelatedForUser', async () => {
    it('should return just the base features when no user related features found', () => {
      const features = [
        {
          properties: {
            type: 'bommenkaart/verdachtgebied' // not in map-search
          }
        }
      ];
      const results = fetchRelatedForUser(user)({ features });
      expect(results).toEqual(features);
    });

    it('should return just the base features when user related features found but user is not authorized', async () => {
      user.scopes = ['CAT/W', 'CAT/R'];
      const features = [
        {
          properties: {
            type: 'bommenkaart/verdachtgebied' // not in map-search
          }
        },
        {
          properties: {
            id: '0363020012061429',
            type: 'bag/ligplaats',
            uri: 'https://acc.api.data.amsterdam.nl/bag/ligplaats/0363020012061429/'
          }
        }
      ];
      const results = await fetchRelatedForUser(user)({ features });
      expect(results).toEqual(features);
    });

    it('should return the user related features for the standplaats', async () => {
      const features = [
        {
          properties: {
            id: '0363020012061429',
            type: 'bag/standplaats',
            uri: 'https://acc.api.data.amsterdam.nl/bag/ligplaats/0363020012061429/'
          }
        }
      ];
      const vestigingResult = {
        _links: { self: { href: 'https://acc.api.data.amsterdam.nl/bag/nummeraanduiding/0363200012109853/' } },
        _display: 'Singel 177',
        landelijk_id: '0363200012109853',
        hoofdadres: true,
        vbo_status: { code: '33', omschrijving: 'Plaats aangewezen' },
        dataset: 'bag'
      };
      address.fetchHoofdadresByStandplaatsId
        .mockImplementation(() => Promise.resolve({ id: 2000 }));
      vestiging.fetchByAddressId
        .mockImplementation(() => Promise.resolve([vestigingResult]));
      const results = await fetchRelatedForUser(user)({ features });
      expect(results).toEqual([...features, {
        ...vestigingResult,
        properties: {
          uri: vestigingResult._links.self.href,
          display: vestigingResult._display,
          type: 'vestiging',
          parent: 'bag/standplaats'
        }
      }]);
    });

    it('should return the user related features for the ligplaats', async () => {
      const features = [
        {
          properties: {
            id: '0363020012061429',
            type: 'bag/ligplaats',
            uri: 'https://acc.api.data.amsterdam.nl/bag/ligplaats/0363020012061429/'
          }
        }
      ];
      const vestigingResult = {
        _links: { self: { href: 'https://acc.api.data.amsterdam.nl/bag/nummeraanduiding/0363200012109853/' } },
        _display: 'Singel 177',
        landelijk_id: '0363200012109853',
        hoofdadres: true,
        vbo_status: { code: '33', omschrijving: 'Plaats aangewezen' },
        dataset: 'bag'
      };
      address.fetchHoofdadresByLigplaatsId
        .mockImplementation(() => Promise.resolve({ id: 1000 }));
      vestiging.fetchByAddressId
        .mockImplementation(() => Promise.resolve([vestigingResult]));
      const results = await fetchRelatedForUser(user)({ features });
      expect(results).toEqual([...features, {
        ...vestigingResult,
        properties: {
          uri: vestigingResult._links.self.href,
          display: vestigingResult._display,
          type: 'vestiging',
          parent: 'bag/ligplaats'
        }
      }]);
    });
  });
});<commit_msg>Removed async keyword in describe method<commit_after>import search, { fetchRelatedForUser } from './map-search';

import * as address from '../../../shared/services/adressen/adressen-nummeraanduiding';
// import * as monument from '../../../shared/services/monument/monument';
import * as vestiging from '../../../shared/services/vestiging/vestiging';

import { getByUrl } from '../../../shared/services/api/api';

jest.mock('../../../shared/services/api/api');
jest.mock('../../../shared/services/adressen/adressen-nummeraanduiding');
jest.mock('../../../shared/services/monument/monument');
jest.mock('../../../shared/services/vestiging/vestiging');

describe('mapSearch service', () => {
  let user;

  beforeEach(() => {
    user = {
      authenticated: true,
      scopes: [
        'HR/R'
      ],
      name: '<EMAIL>'
    };
  });

  describe('search action', () => {
    it('should return results from search for bommen', async () => {
      getByUrl.mockReturnValue(Promise.resolve({
        features: [
          {
            properties: {
              type: 'bommenkaart/verdachtgebied' // not in map-search
            }
          }
        ]
      }));

      const data = await search({ latitude: 1, longitude: 0 }, {
        authenticated: false,
        accessToken: '',
        scopes: [],
        name: '',
        error: false
      });

      expect(data.results).toEqual([{
        categoryLabel: 'Explosief',
        results: Array(9).fill({ // !!!!IMPORTANT: UPDATE WITH +1 WHEN ADDING NEW GEOSEARCH
          categoryLabel: 'Explosief',
          label: undefined,
          parent: undefined,
          statusLabel: 'verdacht gebied',
          type: 'bommenkaart/verdachtgebied',
          uri: undefined
        }),
        subCategories: [],
        type: 'bommenkaart/verdachtgebied'
      }]);
    });

    it('should return results and ignore the ignore the failing calls (http 500 errors)', async () => {
      getByUrl.mockImplementation((url) => {
        if (url.indexOf('/nap/') > 0) {
          return Promise.reject();
        }
        return (
          Promise.resolve({
            features: [
              {
                properties: {
                  type: 'bommenkaart/verdachtgebied' // not in map-search
                }
              }
            ]
          })
        );
      });

      const data = await search({ latitude: 1, longitude: 0 }, {
        authenticated: false,
        accessToken: '',
        scopes: [],
        name: '',
        error: false
      });

      expect(data.results).toEqual([{
        categoryLabel: 'Explosief',
        results: Array(8).fill({ // !!!!IMPORTANT: UPDATE WITH +1 WHEN ADDING NEW GEOSEARCH
          categoryLabel: 'Explosief',
          label: undefined,
          parent: undefined,
          statusLabel: 'verdacht gebied',
          type: 'bommenkaart/verdachtgebied',
          uri: undefined
        }),
        subCategories: [],
        type: 'bommenkaart/verdachtgebied'
      }]);
    });
  });

  describe('fetchRelatedForUser', () => {
    it('should return just the base features when no user related features found', () => {
      const features = [
        {
          properties: {
            type: 'bommenkaart/verdachtgebied' // not in map-search
          }
        }
      ];
      const results = fetchRelatedForUser(user)({ features });
      expect(results).toEqual(features);
    });

    it('should return just the base features when user related features found but user is not authorized', async () => {
      user.scopes = ['CAT/W', 'CAT/R'];
      const features = [
        {
          properties: {
            type: 'bommenkaart/verdachtgebied' // not in map-search
          }
        },
        {
          properties: {
            id: '0363020012061429',
            type: 'bag/ligplaats',
            uri: 'https://acc.api.data.amsterdam.nl/bag/ligplaats/0363020012061429/'
          }
        }
      ];
      const results = await fetchRelatedForUser(user)({ features });
      expect(results).toEqual(features);
    });

    it('should return the user related features for the standplaats', async () => {
      const features = [
        {
          properties: {
            id: '0363020012061429',
            type: 'bag/standplaats',
            uri: 'https://acc.api.data.amsterdam.nl/bag/ligplaats/0363020012061429/'
          }
        }
      ];
      const vestigingResult = {
        _links: { self: { href: 'https://acc.api.data.amsterdam.nl/bag/nummeraanduiding/0363200012109853/' } },
        _display: 'Singel 177',
        landelijk_id: '0363200012109853',
        hoofdadres: true,
        vbo_status: { code: '33', omschrijving: 'Plaats aangewezen' },
        dataset: 'bag'
      };
      address.fetchHoofdadresByStandplaatsId
             .mockImplementation(() => Promise.resolve({ id: 2000 }));
      vestiging.fetchByAddressId
               .mockImplementation(() => Promise.resolve([vestigingResult]));
      const results = await fetchRelatedForUser(user)({ features });
      expect(results).toEqual([...features, {
        ...vestigingResult,
        properties: {
          uri: vestigingResult._links.self.href,
          display: vestigingResult._display,
          type: 'vestiging',
          parent: 'bag/standplaats'
        }
      }]);
    });

    it('should return the user related features for the ligplaats', async () => {
      const features = [
        {
          properties: {
            id: '0363020012061429',
            type: 'bag/ligplaats',
            uri: 'https://acc.api.data.amsterdam.nl/bag/ligplaats/0363020012061429/'
          }
        }
      ];
      const vestigingResult = {
        _links: { self: { href: 'https://acc.api.data.amsterdam.nl/bag/nummeraanduiding/0363200012109853/' } },
        _display: 'Singel 177',
        landelijk_id: '0363200012109853',
        hoofdadres: true,
        vbo_status: { code: '33', omschrijving: 'Plaats aangewezen' },
        dataset: 'bag'
      };
      address.fetchHoofdadresByLigplaatsId
             .mockImplementation(() => Promise.resolve({ id: 1000 }));
      vestiging.fetchByAddressId
               .mockImplementation(() => Promise.resolve([vestigingResult]));
      const results = await fetchRelatedForUser(user)({ features });
      expect(results).toEqual([...features, {
        ...vestigingResult,
        properties: {
          uri: vestigingResult._links.self.href,
          display: vestigingResult._display,
          type: 'vestiging',
          parent: 'bag/ligplaats'
        }
      }]);
    });
  });
});
{'fraction_non_alphanumeric': 0.14386846311943366, 'fraction_numerical': 0.028925934383801477, 'mean_word_length': 1.9398075632132468, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6337225', 'n_tokens_mistral': 4132, 'n_tokens_neox': 3859, 'n_words': 746}