Repository navigation
STORY-142: Reading list (Save for later) UI #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
34faf34
feature: STORY-142 reading list (Save for later) UI
devin-ai-integration[bot] 17e271b
bug: STORY-142 reading list pagination after last-page unsave; Save f…
devin-ai-integration[bot] 68268df
fix: STORY-142 match Reading list design (inline card meta, amber act…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/app/features/article/components/bookmark-button.component.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import 'zone.js'; | ||
| import 'zone.js/testing'; | ||
| import { describe, it, expect, beforeEach, afterEach, beforeAll, vi } from 'vitest'; | ||
| import { ComponentFixture, TestBed, getTestBed } from '@angular/core/testing'; | ||
| import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; | ||
| import { provideHttpClient } from '@angular/common/http'; | ||
| import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; | ||
| import { Router } from '@angular/router'; | ||
| import { BehaviorSubject } from 'rxjs'; | ||
| import { BookmarkButtonComponent } from './bookmark-button.component'; | ||
| import { UserService } from '../../../core/auth/services/user.service'; | ||
| import { Article } from '../models/article.model'; | ||
|
|
||
| describe('BookmarkButtonComponent', () => { | ||
| beforeAll(() => { | ||
| getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); | ||
| }); | ||
|
|
||
| const article: Article = { | ||
| slug: 'how-to-build-webapps-that-scale', | ||
| title: 'How to build webapps that scale', | ||
| description: 'Web development technologies have evolved...', | ||
| body: 'body', | ||
| tagList: [], | ||
| createdAt: '2026-10-27T00:00:00.000Z', | ||
| updatedAt: '2026-10-27T00:00:00.000Z', | ||
| favorited: false, | ||
| favoritesCount: 12, | ||
| bookmarked: false, | ||
| author: { username: 'eric', bio: null, image: '', following: false }, | ||
| }; | ||
|
|
||
| let fixture: ComponentFixture<BookmarkButtonComponent>; | ||
| let httpMock: HttpTestingController; | ||
| let isAuthenticated: BehaviorSubject<boolean>; | ||
| let router: { navigate: ReturnType<typeof vi.fn> }; | ||
| let emitted: boolean[]; | ||
|
|
||
| const button = () => fixture.nativeElement.querySelector('button') as HTMLButtonElement; | ||
|
|
||
| const render = (value: Article) => { | ||
| fixture.componentRef.setInput('article', value); | ||
| fixture.detectChanges(); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| isAuthenticated = new BehaviorSubject(true); | ||
| router = { navigate: vi.fn().mockResolvedValue(true) }; | ||
| TestBed.configureTestingModule({ | ||
| imports: [BookmarkButtonComponent], | ||
| providers: [ | ||
| provideHttpClient(), | ||
| provideHttpClientTesting(), | ||
| { provide: UserService, useValue: { isAuthenticated } }, | ||
| { provide: Router, useValue: router }, | ||
| ], | ||
| }); | ||
| fixture = TestBed.createComponent(BookmarkButtonComponent); | ||
| httpMock = TestBed.inject(HttpTestingController); | ||
| emitted = []; | ||
| fixture.componentInstance.toggle.subscribe(value => emitted.push(value)); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| httpMock.verify(); | ||
| TestBed.resetTestingModule(); | ||
| }); | ||
|
|
||
| it('renders an outline "Save" button when not bookmarked', () => { | ||
| render(article); | ||
| expect(button().textContent?.trim()).toBe('🔖 Save'); | ||
| expect(button().classList).toContain('btn-sm'); | ||
| expect(button().classList).toContain('btn-outline-bookmark'); | ||
| expect(button().classList).not.toContain('btn-bookmark'); | ||
| }); | ||
|
|
||
| it('renders a filled "Saved" button when bookmarked', () => { | ||
| render({ ...article, bookmarked: true }); | ||
| expect(button().textContent?.trim()).toBe('🔖 Saved'); | ||
| expect(button().classList).toContain('btn-bookmark'); | ||
| expect(button().classList).not.toContain('btn-outline-bookmark'); | ||
| }); | ||
|
|
||
| it('treats a missing bookmarked field as not saved', () => { | ||
| const { bookmarked: _ignored, ...withoutField } = article; | ||
| render(withoutField as Article); | ||
| expect(button().textContent?.trim()).toBe('🔖 Save'); | ||
| }); | ||
|
|
||
| it('bookmarks the article when not saved and emits true', () => { | ||
| render(article); | ||
| button().click(); | ||
| const req = httpMock.expectOne(`/articles/${article.slug}/bookmark`); | ||
| expect(req.request.method).toBe('POST'); | ||
| req.flush({ article: { ...article, bookmarked: true } }); | ||
| expect(emitted).toEqual([true]); | ||
| expect(fixture.componentInstance.isSubmitting()).toBe(false); | ||
| }); | ||
|
|
||
| it('unbookmarks the article when saved and emits false', () => { | ||
| render({ ...article, bookmarked: true }); | ||
| button().click(); | ||
| const req = httpMock.expectOne(`/articles/${article.slug}/bookmark`); | ||
| expect(req.request.method).toBe('DELETE'); | ||
| req.flush({ article: { ...article, bookmarked: false } }); | ||
| expect(emitted).toEqual([false]); | ||
| }); | ||
|
|
||
| it('does not emit when the request fails', () => { | ||
| render(article); | ||
| button().click(); | ||
| httpMock.expectOne(`/articles/${article.slug}/bookmark`).flush(null, { status: 500, statusText: 'Error' }); | ||
| expect(emitted).toEqual([]); | ||
| expect(fixture.componentInstance.isSubmitting()).toBe(false); | ||
| }); | ||
|
|
||
| it('redirects anonymous users to /login without calling the API', () => { | ||
| isAuthenticated.next(false); | ||
| render(article); | ||
| button().click(); | ||
| expect(router.navigate).toHaveBeenCalledWith(['/login']); | ||
| httpMock.expectNone(() => true); | ||
| expect(emitted).toEqual([]); | ||
| expect(fixture.componentInstance.isSubmitting()).toBe(false); | ||
| }); | ||
| }); |
105 changes: 105 additions & 0 deletions
105
src/app/features/article/components/bookmark-button.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| DestroyRef, | ||
| EventEmitter, | ||
| inject, | ||
| Input, | ||
| Output, | ||
| signal, | ||
| } from '@angular/core'; | ||
| import { Router } from '@angular/router'; | ||
| import { EMPTY, switchMap, take } from 'rxjs'; | ||
| import { NgClass } from '@angular/common'; | ||
| import { ArticlesService } from '../services/articles.service'; | ||
| import { UserService } from '../../../core/auth/services/user.service'; | ||
| import { Article } from '../models/article.model'; | ||
| import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-bookmark-button', | ||
| template: ` | ||
| <button | ||
| type="button" | ||
| class="btn btn-sm bookmark-btn" | ||
| [ngClass]="{ | ||
| disabled: isSubmitting(), | ||
| 'btn-outline-bookmark': !article.bookmarked, | ||
| 'btn-bookmark': article.bookmarked, | ||
| }" | ||
| [attr.aria-pressed]="!!article.bookmarked" | ||
| (click)="toggleBookmark()" | ||
| > | ||
| 🔖 {{ article.bookmarked ? 'Saved' : 'Save' }} | ||
| </button> | ||
| `, | ||
| styles: ` | ||
| .btn-outline-bookmark { | ||
| color: #f0ad4e; | ||
| background-color: transparent; | ||
| border-color: #f0ad4e; | ||
| } | ||
| .btn-outline-bookmark:hover { | ||
| color: #fff; | ||
| background-color: #f0ad4e; | ||
| border-color: #f0ad4e; | ||
| } | ||
| .btn-bookmark { | ||
| color: #fff; | ||
| background-color: #f0ad4e; | ||
| border-color: #f0ad4e; | ||
| } | ||
| .btn-bookmark:hover, | ||
| .btn-bookmark:focus { | ||
| color: #fff; | ||
| background-color: #ec971f; | ||
| border-color: #eb9316; | ||
| } | ||
| `, | ||
| imports: [NgClass], | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| }) | ||
| export class BookmarkButtonComponent { | ||
| destroyRef = inject(DestroyRef); | ||
| isSubmitting = signal(false); | ||
|
|
||
| @Input() article!: Article; | ||
| @Output() toggle = new EventEmitter<boolean>(); | ||
|
|
||
| constructor( | ||
| private readonly articleService: ArticlesService, | ||
| private readonly router: Router, | ||
| private readonly userService: UserService, | ||
| ) {} | ||
|
|
||
| toggleBookmark(): void { | ||
| const wasBookmarked = !!this.article.bookmarked; | ||
| this.isSubmitting.set(true); | ||
|
|
||
| this.userService.isAuthenticated | ||
| .pipe( | ||
| take(1), | ||
| switchMap(authenticated => { | ||
| if (!authenticated) { | ||
| this.isSubmitting.set(false); | ||
| void this.router.navigate(['/login']); | ||
| return EMPTY; | ||
| } | ||
|
|
||
| return wasBookmarked | ||
| ? this.articleService.unbookmark(this.article.slug) | ||
| : this.articleService.bookmark(this.article.slug); | ||
| }), | ||
| takeUntilDestroyed(this.destroyRef), | ||
| ) | ||
| .subscribe({ | ||
| next: () => { | ||
| this.isSubmitting.set(false); | ||
| this.toggle.emit(!wasBookmarked); | ||
| }, | ||
| error: () => { | ||
| this.isSubmitting.set(false); | ||
| }, | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.