Is there a way to verify the content inside the :before selector using Jasmine within an Angular 6 application?

Looking to test whether the content of a label changes based on the checkbox being checked using Jasmine in an Angular 6 project. Is this feasible?

Code snippet:

HTML

<input id="myCheck" class="checkbox" type="checkbox" />
<label for="myCheck" class="label"></label>

CSS

.label{
    color: black;
    font-size: 14px;
}
.label:before{
    content: 'NOT Checked';
}

.checkbox {
    display: none;
}
.checkbox:checked + label:before {
    content: 'Checked';
}

Jasmine Unit Test

it('should say "Checked" when checkbox is checked and "NOT Checked" when not checked', () => {
    const check = el.query(By.css('#myCheck')).nativeElement;
    expect(el.query(By.css('#myCheck + label:before'))).toBe('NOT Checked');

    check.click();
    fixture.detectChanges();

    expect(el.query(By.css('#myCheck + label:before'))).toBe('Checked');
});

Answer №1

Discovered a new method to access the :before selector by utilizing the window object and getComputedStyle:

it('should display "Checked" when the checkbox is checked and "NOT Checked" when unchecked', () => {
    const checkbox = el.query(By.css('#myCheck')).nativeElement;
    let textContent = window.getComputedStyle('.label')).nativeElement, ':before').getPropertyValue('content');
    expect(textContent).toBe('NOT Checked');

    checkbox.click();
    fixture.detectChanges();
    textContent = window.getComputedStyle('.label')).nativeElement, ':before').getPropertyValue('content');
    expect(textContent).toBe('Checked');
});

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 surprising outcome of altering the background color of a div during a click event

Currently, I am engrossed in crafting a word guessing game. In this game, I've constructed a visual keyboard. When a user selects a letter on this keyboard, I desire the background color of that specific letter to adjust accordingly - light green if t ...

Refresh a table using jQuery Mobile, PHP, and AJAX without having to reload the entire page by clicking a

Currently, I am working on a web app that includes a pop-up feature. When the user clicks on the pop-up to close it, I want the table data to refresh without having to reload the entire page. This pop-up allows users to modify existing data in the table. A ...

Incorporate Facebook Friends into Your Website Using Graph API 2.5

In the midst of creating a website where users can connect with their Facebook friends, I encountered a challenge regarding utilizing apprequests. Unfortunately, this feature is only accessible to games from version 2.3 onwards. At present, my solution inv ...

How can the presence of a CSS rule prevent another from being applied?

My attempt to create this HTML file in Chrome posed some challenges: <style> br { }​ [required] { border-width: 3px; border-color: red; } </style> <input required /> The entire content of the file is shown above. However, I noti ...

`Angular RxJS vs Vue Reactivity: Best practices for managing UI updates that rely on timers`

How can you implement a loading spinner following an HTTP request, or any asynchronous operation that occurs over time, using the specified logic? Wait for X seconds (100ms) and display nothing. If the data arrives within X seconds (100ms), display i ...

Height of Hover Background Color in WordPress Menu

Greetings! Welcome to my website I'm in the process of modifying the hover color for the top menu, and I've managed to change it successfully. However, I would like the hover effect to cover the entire height of the menu, as right now it's ...

React event handling

Currently, I am in the process of developing an accordion app using React. The data for this app is fetched from an API, and while I have the basic structure of the app ready, I am facing some difficulties with handling the click event on the accordion. H ...

The issue of CSS Grid not adapting fully to different screen sizes and

Recently, I took on a coding challenge from Frontend Mentor (this one) and managed to complete it successfully. However, the grid I designed is not fully responsive. It only adapts correctly when the page width matches the media query specifications. The c ...

how to combine a string in JavaScript using a prefix and suffix

Anion Gap Body Surface Area (BSA) Creatinine Clearance Stack Overflow i want to add "String" Before Text and "String" after end of text. I have Following expression which runs perfectly in excel:- =CONCATENATE("",B1,"") Output:- <string>Anion Ga ...

Why is it unnecessary to include a callback when using a setState function from useState as an argument in useEffect?

While working with a function from my component in the useEffect arguments, it is recommended to write a callBack so that it is memorized and used as a dependency of the useEffect. Without this, there is a warning. However, when using the setState of use ...

Tips for positioning a div element at the center in ASP.NET

I have written the following code snippet: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit ...

Manipulate Input Classes by Adding and Removing Them

I am currently using masked JS in some input fields, specifically for telephone numbers. However, I am facing an issue where phone numbers can vary in length, either 10 or 11 digits. I need to switch the class based on whether a checkbox is checked. Here i ...

It seems like the method has already been executed despite encountering an unexpected end of JSON input error

Currently, I am utilizing the etherscan API to retrieve logs for certain events. Although my JSON parsing method seems quite traditional, an error is being thrown stating "unexpected end of JSON". function getEventHistory() { const topic0 = web3.util ...

Converting cURL commands to work with Angular versions 2, 4, or 5: A

Can you help me convert this cURL command to Angular 2, 4, or 5 for connecting to an API? curl -k -i -X GET -H "Content-Type: application/json" -u username:"password" https://myRESTFULL_API_URL I was thinking of using something like th ...

File input onChange event not triggering after pressing SPACE or ENTER key

My React component features an img tag that allows users to select an image from their computer to display in it. While this component functions correctly on most browsers, I encountered an issue specifically with Chromium based browsers (tested on Chrome, ...

Error encountered in VUE JS 3: The function vue.initDirectivesForSSR is not recognized as a valid function

I've been encountering some errors while trying to build my Vue web app with this configuration. I have searched for a solution, but couldn't find anyone facing the same issue as me. Any suggestions on how to resolve this problem? The build was s ...

Error: Document's _id field cannot be modified

I am new to both MongoDB and Backbone, and I find it challenging to grasp the concepts. My main issue revolves around manipulating attributes in Backbone.Model to efficiently use only the necessary data in Views. Specifically, I have a model: window.User ...

Working with base URL and relative paths in Angular 2 on GitHub Pages

While attempting to deploy my project site on gh-pages, I encountered an issue with relative paths in the templates of my Angular2 app that uses webpack. Despite setting the base URL to match my repository name, everything loads fine except for the paths w ...

Sign-in options displayed in a drop-down menu

I have successfully implemented a jQuery animation for a dropdown sign in div. The sign up form is integrated with PHP to verify the existence of users in the database. However, I came across an issue where if I echo something, the dropdown menu disappears ...

I am not getting any emails when users click on the "Pay Now" button

I am currently learning PHP and working on a website that integrates with the paytm gateway. I have a registration page with a "Pay Now" button, but when someone clicks on it, they are directed to pgRedirect.php (provided by paytm). I would like to recei ...