最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I check the innerHTML content of a paragraph using Jasmine and Karma in Angular - Stack Overflow

programmeradmin3浏览0评论

I have an Angular component that has a <p> tag with some text. One of the sentences inside of the paragraph is bold. I am trying to write a unit test in Jasmine/Karma to check the innerHTML content. However, all of my attempts seem to fail. Am I going about this the wrong way? Here is my markup and JavaScript:

Angular component HTML

<p>
    Four score and seven years ago our fathers brought forth on this 
    continent, <b>a new nation</b>, conceived in Liberty, and dedicated to the 
    proposition that all men are created equal.
</p>

Jasmine Unit Test

describe('TestComponent', () => {
    let component: TestComponent;
    let fixture: ComponentFixture<TestComponent>;
    let el: DebugElement;

    beforeEach(() => {
        fixture = TestBed.createComponent(TestComponent);
        component = fixtureponentInstance;
        el = fixture.debugElement;
        fixture.detectChanges();
    });

    it('should display "a new nation" as bold text', () => {
        const boldText = el.query(By.css('p')).nativeElement;
        expect(boldText.innerHTML).toContain('<b>a new nation</b>');
    });
});

I get the following failed error with this code:

Expected 'Four score and seven years ago our fathers brought forth on this continent, <b _ngcontent-c1="">a new nation</b>, conceived in Liberty, and dedicated to the proposition that all men are created equal.'.

Note that Angular is injecting its own ID to the <b> tag. Is this the reason is fails? Is this even the best way to go about this kind of test?

Thanks!

I have an Angular component that has a <p> tag with some text. One of the sentences inside of the paragraph is bold. I am trying to write a unit test in Jasmine/Karma to check the innerHTML content. However, all of my attempts seem to fail. Am I going about this the wrong way? Here is my markup and JavaScript:

Angular component HTML

<p>
    Four score and seven years ago our fathers brought forth on this 
    continent, <b>a new nation</b>, conceived in Liberty, and dedicated to the 
    proposition that all men are created equal.
</p>

Jasmine Unit Test

describe('TestComponent', () => {
    let component: TestComponent;
    let fixture: ComponentFixture<TestComponent>;
    let el: DebugElement;

    beforeEach(() => {
        fixture = TestBed.createComponent(TestComponent);
        component = fixture.componentInstance;
        el = fixture.debugElement;
        fixture.detectChanges();
    });

    it('should display "a new nation" as bold text', () => {
        const boldText = el.query(By.css('p')).nativeElement;
        expect(boldText.innerHTML).toContain('<b>a new nation</b>');
    });
});

I get the following failed error with this code:

Expected 'Four score and seven years ago our fathers brought forth on this continent, <b _ngcontent-c1="">a new nation</b>, conceived in Liberty, and dedicated to the proposition that all men are created equal.'.

Note that Angular is injecting its own ID to the <b> tag. Is this the reason is fails? Is this even the best way to go about this kind of test?

Thanks!

Share Improve this question asked Jul 10, 2018 at 13:49 J-manJ-man 1,8333 gold badges28 silver badges52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

Query directly the bold text :

const boldText = el.query(By.css('p b')).nativeElement;
expect(boldText.textContent).toContain('a new nation');

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论