import { GoogleMapsScriptProtocol, LazyMapsAPILoader } from "./google-maps-loader";
describe('Service: LazyMapsAPILoader', () => {
    let loader;
    beforeEach(() => {
        document = {
            createElement: jest.fn(),
            getElementById: jest.fn().mockReturnValue(null),
            body: {
                appendChild: jest.fn()
            }
        };
        loader = new LazyMapsAPILoader();
    });
    it('should create the default script URL', () => {
        const scriptElem = {};
        document.createElement.mockReturnValue(scriptElem);
        loader.load();
        expect(document.createElement).toHaveBeenCalledWith('script');
        expect(scriptElem.type).toEqual('text/javascript');
        expect(scriptElem.async).toEqual(true);
        expect(scriptElem.defer).toEqual(true);
        expect(scriptElem.src).toBeDefined();
        expect(scriptElem.id).toEqual('bpSdkGoogleMapsApiScript');
        expect(scriptElem.src).toContain('https://maps.googleapis.com/maps/api/js');
        expect(scriptElem.src).toContain('v=quarterly');
        expect(scriptElem.src).toContain('callback=bpSdkLazyMapsAPILoader');
        expect(document.body.appendChild).toHaveBeenCalledWith(scriptElem);
    });
    it('should not append a second script to body when theres already one with the fixed ID', () => {
        (loader) => {
            document.getElementById.mockReturnValue(document.createElement('script'));
            loader.load();
            expect(document.body.appendChild).not.toHaveBeenCalledWith();
        };
    });
    it('should not append a second script to body when window.google.maps is defined', () => {
        window.google = {
            maps: {}
        };
        loader.load();
        expect(document.body.appendChild).not.toHaveBeenCalledWith();
    });
    it('should load the script via http when provided', () => {
        loader.protocol = GoogleMapsScriptProtocol.HTTP;
        const scriptElem = {};
        document.createElement.mockReturnValue(scriptElem);
        loader.load();
        expect(document.createElement).toHaveBeenCalled();
        expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js');
        expect(document.body.appendChild).toHaveBeenCalledWith(scriptElem);
    });
    it('should load language based on locale', () => {
        loader.localeId = 'es';
        const scriptElem = {};
        document.createElement.mockReturnValue(scriptElem);
        loader.load();
        expect(document.createElement).toHaveBeenCalled();
        const url = new URL(scriptElem.src);
        expect(url.searchParams.get('language')).toEqual('es');
    });
    it('should load language based on config', () => {
        loader.language = 'tr';
        loader.localeId = 'es';
        const scriptElem = {};
        document.createElement.mockReturnValue(scriptElem);
        loader.load();
        expect(document.createElement).toHaveBeenCalled();
        const url = new URL(scriptElem.src);
        expect(url.searchParams.get('language')).toEqual('tr');
    });
});
