Skip to content

Commit 45edefe

Browse files
committed
refactor test using selector
1 parent c740f30 commit 45edefe

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/app/components/user-stats/user-stats.component.spec.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,17 @@ describe('UserStatsComponent', () => {
8787
const header = getDOM().querySelector(el, 'h2');
8888
expect(header.innerText).toMatch(/test1/);
8989

90-
const profileLink = getDOM().querySelector(el, 'section:first-child a');
90+
const profileLink = getDOM().querySelector(el, 'a[href="/users/1"]');
9191
expect(profileLink).toBeTruthy();
92-
expect(profileLink.getAttribute('href')).toEqual('/users/1');
9392

94-
const postCnt = getDOM().querySelector(el, 'section:first-child > span:last-child');
95-
expect(postCnt).toBeTruthy();
96-
expect(postCnt.innerText).toMatch(/2 microposts/);
93+
expect(el.innerText).toMatch(/2 microposts/);
9794

98-
const followingsLink = getDOM().querySelector(el, 'section:last-child a:first-child');
95+
const followingsLink = getDOM().querySelector(el, 'a[href="/users/1/followings"]');
9996
expect(followingsLink).toBeTruthy();
100-
expect(followingsLink.getAttribute('href')).toEqual('/users/1/followings');
10197
expect(followingsLink.innerText).toMatch(/3[\s\S]*?followings/);
10298

103-
const followersLink = getDOM().querySelector(el, 'section:last-child a:last-child');
99+
const followersLink = getDOM().querySelector(el, 'a[href="/users/1/followers"]');
104100
expect(followersLink).toBeTruthy();
105-
expect(followersLink.getAttribute('href')).toEqual('/users/1/followers');
106101
expect(followersLink.innerText).toMatch(/4[\s\S]*?followers/);
107102
});
108103

@@ -114,13 +109,13 @@ describe('UserStatsComponent', () => {
114109
});
115110

116111
it('does not show profile link', () => {
117-
const profileLink = getDOM().querySelector(cmpDebugElement.nativeElement, 'a.profile-link');
118-
expect(profileLink).toBeFalsy();
112+
const el = cmpDebugElement.nativeElement;
113+
expect(el.innerText).not.toMatch(/view my profile/);
119114
});
120115

121116
it('does not show post counts', () => {
122-
const postCnt = getDOM().querySelector(cmpDebugElement.nativeElement, '.microposts');
123-
expect(postCnt).toBeFalsy();
117+
const el = cmpDebugElement.nativeElement;
118+
expect(el.innerText).not.toMatch(/\d+ microposts?/);
124119
});
125120
});
126121

0 commit comments

Comments
 (0)