What methods should I employ to effectively test a custom icon function?

I've written a function that creates a Leaflet icon with specified properties:

createIcon(
    url,
    retinaUrl: string = null,
    height: number = 20,
    width: number = 20
  ): Icon {
    const icon: Icon = L.icon({
      iconUrl: url,
      iconRetinaUrl: retinaUrl,
      iconSize: [width, height], 
      shadowSize: [0, 0],
      iconAnchor: [width / 2, height / 2], 
      shadowAnchor: [0, 0], 
      popupAnchor: [0, 0]
    });
    return icon;
  }

Now, let's test this function:

it('should create an icon with specified properties', () => {
    const testURL = 'testUrl';
    const testRetinaURL = 'test';
    const testHeight = 2;
    const testWidth = 2;
    expect(
      service.createIcon(testURL, testRetinaURL, testHeight, testWidth)
    ).toEqual(expectedIcon);
  });

Here is the expected icon object we are comparing against:

const expectedIcon: Icon = L.icon({
    iconUrl: 'testUrl',
    iconRetinaUrl: 'test',
    iconSize: [2, 2],
    shadowSize: [0, 0],
    iconAnchor: [20 / 2, 20 / 2],
    shadowAnchor: [0, 0],
    popupAnchor: [0, 0]
  });

While testing, I encountered an error message:

Expected $.options.iconAnchor[0] = 1 to equal 10.
Expected $.options.iconAnchor[1] = 1 to equal 10.

It seems like the anchor values are not matching. How can I set these additional properties when the function only accepts four parameters?

Any suggestions on improving the testing process for this function?

Answer №1

It appears that there might be an issue with your current setup. The variable "anotherTestIcon" seems to be passed in without being defined first.

To resolve this, you can refer to a working example here: https://stackblitz.com/edit/github-test-run-draft-fn737v?file=src/app/app.component.spec.ts

You can modify your test case to resemble the following:

  it('should return a simple icon with properties', () => {
    const anotherTestIcon = {
      iconUrl: 'foop',
      iconRetinaUrl: 'doop',
      iconSize: [1, 1],
      shadowSize: [0, 0],
      iconAnchor: [1 / 2, 1 / 2],
      shadowAnchor: [0, 0],
      popupAnchor: [0, 0],
    };

    const result = component.addSimpleIcon(
      anotherTestIcon.iconUrl,
      anotherTestIcon.iconRetinaUrl,
      1,
      1
    );

    expect(result).toEqual(anotherTestIcon);
  });

Answer №2

Discovered that the reason this test was failing is because I mistakenly compared an object to an L.icon.

Answer №3

By updating the following:

const sampleHeight = 2;
const sampleWidth = 2;

To this:

const sampleHeight = 20;
const sampleWidth = 20;

The functionality will be restored.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The issue arises when attempting to send requests from an Ionic 5 project on the iOS platform, as the session does not seem to be functioning properly. Strang

On the backend, I have set up a Java server and linked it to my ionic 5 project if(origin.contains("ionic")) { httpresponse.setHeader("Access-Control-Allow-Origin", "ionic://localhost"); } else { httpre ...

Creating a class and initializing it, then implementing it in other classes within an Angular application

Trying to grasp some angular fundamentals by creating a class structure. Unsure if this is the right approach. export class Cars{ Items:Car[]=[]; constructor() { this.Items = [ { id: "1", name: "Honda" ...

How can I access the directive model value in its primary controller within an Angular2 application?

I am working on a person page that utilizes a custom directive. <person-directive *ngFor="let person of persons; #idx = index" (remove) = "removePerson(idx)"> </person-directive> The person directive includes input fields as shown bel ...

Attaching a function to a designated slot attribute

Currently, I am utilizing VUE 2.6.11 along with class components. My current objective involves encapsulating components that can serve as modals inside a separate component responsible for managing the modal state. According to the documentation, it is p ...

What is the best way to ensure that multiple queries are returned in the correct sequence?

In the interface below, there is a search input box along with data displayed. As you type in the search input, the data below filters accordingly. Each letter typed triggers a request to retrieve the relevant data. For instance, if you type "folder," the ...

The TypeScript package encountered an unexpected token export

I have integrated a module from a private git repository. Package.json: "my-module": "git+https://username:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebeb98eaca7baacbbada5abbae0a1bca9">[email protected]</a> ...

The error message "TypeError: text is not a function" is displayed when trying to utilize the text() method from either Blob

My current dilemma revolves around the usage of functions defined in the typescript lib.dom.d.ts file within my node.js express backend that is implemented using TypeScript. Specifically, I am encountering issues with the File/Blob interfaces where a File ...

Troubleshooting Angular 2 Final Release Visual Studio - encountering issues with finding module name and incorrect module.id path leading to 404 errors

After upgrading to the Angular 2 Final Release (I am using Visual Studio 2015 and TypeScript 1.8), I noticed that my line moduleId: module.id in my components now has a red squiggly underline and displays an error saying cannot find name 'module' ...

TypeScript compilation error - No overload is compatible with this call

Currently, I am working on a project using TypeScript alongside NodeJS and Express. this.app.listen(port, (err: any) => { if (err) { console.log("err", err) } else { console.log(`Server is listing on port ${port}`); } }); The co ...

What is the process to enable mandatory validation after a change in input in Angular 4?

Currently, I am working on a project using Angular 4. One of the tasks I need to achieve is validation. <input [(ngModel)]="someModel" required placeholder="some placeholder"/> The validation triggers immediately, but I want it to only trigger aft ...

Is it necessary for me to set up @types/node? It appears that VSCode comes with it pre-installed

Many individuals have been adding @types/node to their development dependencies. Yet, if you were to open a blank folder in VSCode and create an empty JavaScript file, then input: const fs = require('fs'); // <= hover it and the type display ...

Is ngOnDestroy executed within Angular services?

Is there a way to confirm if the ngOnDestroy method in my UserServiceService class is functioning properly? I want this service to continue running until the project starts and ends, with ngondestroy method executing once the application (website) is clo ...

Having trouble decoding a cookie received from a React.js front-end on an Express server

When using React js for my front end, I decided to set a cookie using the react-cookie package. After confirming that the request cookie is successfully being set, I moved on to configure the Express server with the cookie parser middleware. app.use(cookie ...

Angular transitions do not smoothly animate when a new component is added

Exploring ways to animate the appearance and disappearance of a new Angular component in my application has been quite challenging. Referencing the guidelines provided by Angular itself at https://angular.io/guide/transition-and-triggers#enter-and-leave- ...

Issues with typescript compiler when using React-beautiful-dnd

I recently updated react and react-beautiful-dnd to the newest versions and now I am encountering many type errors in my code: {sortedDimensions.map((dimension: any, index: number) => ( <Draggable key={index} ...

I am facing an issue with Angular reactive forms where the default values I set in ngOnInIt are not being reflected

Having some issues with setting default values in my Angular app using reactive forms. The defaults I set in ngOnInit are not showing up. I am also using the filter function within the map method. I am trying to select a value based on the URL and have it ...

Using `publishReplay()` and `refCount()` in Angular does not behave as anticipated when dealing with subscriptions across multiple components

I am currently investigating the functionality of publishReplay in rxjs. I have encountered an example where it behaves as expected: const source = new Subject() const sourceWrapper = source.pipe( publishReplay(1), refCount() ) const subscribeTest1 = ...

Creating horizontal cards with Angular MaterialWould you like to learn how to design

I need help converting these vertical cards into horizontal cards. Currently, I am utilizing the Angular Material Cards component. While I can successfully create cards in a vertical layout, my goal is to arrange them horizontally. Below is the code sni ...

Tips on organizing a typescript object/StringMap in reverse order to prioritize the last element

I've just started working with TS/React in a .tsx file and I'm trying to add a key/value pair to a StringMap at the very first index. Below is the code snippet that: takes the StringMap 'stats' as input, iterates through each row, re ...

Can you explain the significance of the | symbol in TypeScript?

My journey with TypeScript is just beginning, and I recently encountered the symbol | while working on a problem in LeetCode using Typescript. I believe it has something to do with defining variable types. Can anyone provide more insight into this? /** ...