@@ -363,6 +363,193 @@ describe('google-maps-services-js files tests', () => {
363363 });
364364 ` )
365365 ) ;
366+ } )
367+
368+ test ( 'serialize.test.ts' , ( ) => {
369+ rewriteRun (
370+ //language=typescript
371+ typeScript ( `
372+ /**
373+ * Copyright 2020 Google LLC
374+ *
375+ * Licensed under the Apache License, Version 2.0 (the "License");
376+ * you may not use this file except in compliance with the License.
377+ * You may obtain a copy of the License at
378+ *
379+ * http://www.apache.org/licenses/LICENSE-2.0
380+ *
381+ * Unless required by applicable law or agreed to in writing, software
382+ * distributed under the License is distributed on an "AS IS" BASIS,
383+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
384+ * See the License for the specific language governing permissions and
385+ * limitations under the License.
386+ */
387+
388+ import { LatLng, LatLngLiteral } from "./common";
389+ import {
390+ createPremiumPlanQueryString,
391+ latLngArrayToStringMaybeEncoded,
392+ latLngBoundsToString,
393+ latLngToString,
394+ objectToString,
395+ serializer,
396+ toLatLngLiteral,
397+ toTimestamp,
398+ } from "./serialize";
399+
400+ test("latLngToString is correct", () => {
401+ expect(latLngToString("")).toBe("");
402+ expect(latLngToString("10,20")).toBe("10,20");
403+ expect(latLngToString([10, 20])).toBe("10,20");
404+ expect(latLngToString({ lat: 10, lng: 20 })).toBe("10,20");
405+ expect(latLngToString({ latitude: 10, longitude: 20 })).toBe("10,20");
406+ expect(() => {
407+ latLngToString({} as LatLngLiteral);
408+ }).toThrow(TypeError);
409+ });
410+
411+ test("latLngBoundsToString is correct", () => {
412+ expect(latLngBoundsToString("")).toBe("");
413+ expect(
414+ latLngBoundsToString({
415+ southwest: { lat: 1, lng: 2 },
416+ northeast: { lat: 3, lng: 4 },
417+ })
418+ ).toBe("1,2|3,4");
419+ });
420+
421+ test("serializer", () => {
422+ expect(
423+ serializer({ quz: (o) => o }, "http://mock.url")({ foo: ["bar"] })
424+ ).toBe("foo=bar");
425+ expect(
426+ serializer(
427+ {
428+ foo: (o) => o.map((latLng: LatLng) => latLngToString(latLng)),
429+ },
430+ "http://mock.url"
431+ )({
432+ foo: [
433+ [0, 1],
434+ [2, 3],
435+ ],
436+ })
437+ ).toBe("foo=0%2C1|2%2C3");
438+ });
439+
440+ test("serializer should not mutate params", () => {
441+ const location = { lat: 0, lng: 1 };
442+ const params = {
443+ location,
444+ };
445+
446+ serializer({ location: latLngToString }, "http://mock.url")(params);
447+ expect(params.location).toBe(location);
448+ });
449+
450+ test("serializer should return pipe joined arrays by default", () => {
451+ expect(serializer({}, "http://mock.url")({ foo: ["b", "a", "r"] })).toBe(
452+ "foo=b|a|r"
453+ );
454+ });
455+
456+ test("serializer creates premium plan query string if premium plan params are included", () => {
457+ const params = {
458+ avoid: "ferries",
459+ destination: {
460+ lat: "38.8977",
461+ lng: "-77.0365",
462+ },
463+ mode: "driving",
464+ origin: {
465+ lat: "33.8121",
466+ lng: "-117.9190",
467+ },
468+ units: "imperial",
469+ client_id: "testClient",
470+ client_secret: "testClientSecret",
471+ };
472+
473+ expect(
474+ serializer(
475+ {
476+ origin: latLngToString,
477+ destination: latLngToString,
478+ },
479+ "https://test.url/maps/api/directions/json"
480+ )(params)
481+ ).toEqual(
482+ "avoid=ferries&client=testClient&destination=38.8977%2C-77.0365&mode=driving&origin=33.8121%2C-117.9190&units=imperial&signature=YRJoTd6ohbpsR14WkWv3S7H6MqU="
483+ );
484+ });
485+
486+ test("objectToString", () => {
487+ expect(objectToString("foo")).toBe("foo");
488+ expect(objectToString({ c: "c", a: "a", b: "b" })).toBe("a:a|b:b|c:c");
489+ });
490+
491+ test("latLngArrayToStringMaybeEncoded", () => {
492+ expect(latLngArrayToStringMaybeEncoded("0,0")).toEqual("0,0");
493+ expect(latLngArrayToStringMaybeEncoded([[0, 0]])).toEqual("0,0");
494+ expect(
495+ latLngArrayToStringMaybeEncoded([
496+ [40.714728, -73.998672],
497+ [-34.397, 150.644],
498+ ])
499+ ).toEqual("enc:abowFtzsbMhgmiMuobzi@");
500+ });
501+
502+ test("toLatLngLiteral", () => {
503+ expect(toLatLngLiteral("0,1")).toEqual({ lat: 0, lng: 1 });
504+ expect(toLatLngLiteral([0, 1])).toEqual({ lat: 0, lng: 1 });
505+ expect(toLatLngLiteral({ lat: 0, lng: 1 })).toEqual({
506+ lat: 0,
507+ lng: 1,
508+ });
509+ expect(toLatLngLiteral({ latitude: 0, longitude: 1 })).toEqual({
510+ lat: 0,
511+ lng: 1,
512+ });
513+ expect(() => {
514+ toLatLngLiteral({} as LatLngLiteral);
515+ }).toThrow(TypeError);
516+ });
517+
518+ test("toTimestamp", () => {
519+ expect(toTimestamp(100)).toEqual(100);
520+
521+ const dt = new Date();
522+ const seconds = Math.round(Number(dt) / 1000);
523+ expect(toTimestamp(dt)).toEqual(seconds);
524+ expect(toTimestamp("now")).toEqual("now");
525+
526+ expect(toTimestamp(new Date("2022-06-22T09:03:33.430Z"))).toEqual(1655888613);
527+ });
528+
529+ test("createPremiumPlanQueryString", () => {
530+ const serializedParams = {
531+ avoid: "ferries",
532+ destination: "38.8977,-77.0365",
533+ mode: "driving",
534+ origin: "33.8121,-117.9190",
535+ units: "imperial",
536+ client_id: "testClient",
537+ client_secret: "testClientSecret",
538+ };
539+ const queryStringOptions = {
540+ arrayFormat: "separator",
541+ arrayFormatSeparator: "|",
542+ };
543+ const baseUrl = "https://test.url/maps/api/directions/json";
544+
545+ expect(
546+ createPremiumPlanQueryString(serializedParams, queryStringOptions, baseUrl)
547+ ).toEqual(
548+ "avoid=ferries&client=testClient&destination=38.8977%2C-77.0365&mode=driving&origin=33.8121%2C-117.9190&units=imperial&signature=YRJoTd6ohbpsR14WkWv3S7H6MqU="
549+ );
550+ });
551+ ` )
552+ ) ;
366553 } ) ;
367554
368555} ) ;
0 commit comments