henrythasler / Leaflet.Geodesic

Add-on to draw geodesic lines with leaflet
GNU General Public License v3.0
155 stars 27 forks source link

unit-test fails in latlngExpressionArraytoLatLngArray() #84

Closed henrythasler closed 2 years ago

henrythasler commented 2 years ago

see PR #83

Test latlngExpressionArraytoLatLngArray › 1D-Array - unknown Object (string instead of number):

    Message:
      expected [Function] to throw an error

    Difference:

      Comparing two different types of values. Expected null but received undefined.

      126 |
      127 |     it("1D-Array - unknown Object (string instead of number)", function () {
    > 128 |         expect(() => latlngExpressionArraytoLatLngArray([{ lat: Berlin.lat, lng: "matafokka" } as any])).to.throw(/Unknown object found/);
          |                                                                                                                  ^
      129 |     });
      130 |
      131 |     it("2D-Array - unknown Object (string instead of number)", function () {

      at Object.<anonymous> (spec/types-helper.test.ts:128:114)
henrythasler commented 2 years ago

@matafokka: This is your modification. Maybe you have an idea why this happens.

ps: I have reverted the change for now

matafokka commented 2 years ago

The test is wrong. This test should be right:

it("1D-Array - unknown Object (string instead of number)", function () {
    try {
        latlngExpressionArraytoLatLngArray([{ lat: Berlin.lat, lng: "matafokka" } as any]);
    } catch (e) {
        expect(e).to.be.an("Error");
        expect(e.message).to.have.match(/Unknown object found/);
    }
});

I have no experience with Jest, so I took one of your other working tests and modified it.

Moreover, in browser it throws for both 2D and 1D array with the same { lat: Berlin.lat, lng: "matafokka"} object.

matafokka commented 2 years ago

No, sorry, your test seems right, let me take a deeper look

matafokka commented 2 years ago

Fixed it. Looks not as good as I wanted initially, but seems to get the job done. Passes both old and new tests.

export function latlngExpressionArraytoLatLngArray(input: L.LatLngExpression[] | L.LatLngExpression[][]): L.LatLng[][] {
    let latlng: L.LatLng[][] = [],
            iterateOver = (instanceOfLatLngExpression(input[0]) ? [input] : input),
            error = new Error("L.LatLngExpression[] | L.LatLngExpression[][] expected. Unknown object found.");

    if (!(iterateOver instanceof Array)) {
        throw error;
    }

    for (let group of iterateOver as L.LatLngExpression[][]) {

        if (!(group instanceof Array)) {
            throw error;
        }

        let sub = [];
        for (let point of group) {
            if (!instanceOfLatLngExpression(point)) {
                throw error;
            }
            sub.push(latlngExpressiontoLatLng(point));
        }
        latlng.push(sub);
    }
    return latlng;
}