Skip to content

Instantly share code, notes, and snippets.

View thecodinganalyst's full-sized avatar
🏠
Working from home

Dennis Cai thecodinganalyst

🏠
Working from home
View GitHub Profile
class HashMapDemo {
public static void main(String[ ] args) {
String key = "superman";
int size = 15;
System.out.println("Key: " + key);
int hashCode = key.hashCode();
System.out.println("HashCode: " + hashCode);
System.out.println("HashCode (Binary): " + Integer.toBinaryString(hashCode));
int h;
int hash = (h = key.hashCode()) ^ (h >>> 16);
describe('getBooks action', function () {
it("should call getBooks and redirect to booksLoaded action", (done) => {
spyOn(booksService, "getBooks").and.returnValue(of(books))
actions$ = of(BookStoreActions.loadBooks);
effects.loadBooks$.subscribe(res => {
expect(booksService.getBooks).toHaveBeenCalled()
expect(res).toEqual(BookStoreActions.booksLoaded({books: books}))
done()
})
})
loadBooks$ = createEffect(() => this.actions$.pipe(
ofType(BookStoreActions.loadBooks),
mergeMap(() => this.booksService.getBooks().pipe(
map(books => BookStoreActions.booksLoaded({books})),
catchError(() => EMPTY)
))
));
@thecodinganalyst
thecodinganalyst / books.effects.spec.ts
Created June 16, 2022 14:01
Angular NgRx Effects TestBed ConfigureTestingModule
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
BooksEffects,
provideMockActions(() => actions$),
provideMockStore({initialState}),
],
imports: [HttpClientModule]
})
booksService = TestBed.inject(BooksService)
@thecodinganalyst
thecodinganalyst / app.component.spec.ts
Created June 16, 2022 13:59
Angular TestBed Configure
let store: MockStore<AppState>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AppComponent, ItemComponent],
imports: [ReactiveFormsModule],
providers: [provideMockStore()],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
});
fixture = TestBed.createComponent(AppComponent);
app = fixture.componentInstance;
ngOnInit(): void{
this.loadBooks();
this.store.select(Selector.books).subscribe(books => this.books$ = books);
this.store.select(Selector.selectedBook).subscribe(book => {
this.selectedBook = book;
});
this.store.select(Selector.showDetail).subscribe(showDetail => {
this.showDetail = showDetail;
showDetail ? this.showPopup() : this.hidePopup();
});
@thecodinganalyst
thecodinganalyst / books.app.module2.ts
Created June 13, 2022 01:31
NgRx module with Store and Effects
@NgModule({
declarations: [
AppComponent,
ItemComponent
],
imports: [
BrowserModule,
HttpClientModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService, {dataEncapsulation: false}),
ReactiveFormsModule,
@thecodinganalyst
thecodinganalyst / books.app.module1.ts
Created June 13, 2022 01:30
NgRx Module with Store Only
@NgModule({
declarations: [
AppComponent,
ItemComponent
],
imports: [
BrowserModule,
HttpClientModule,
HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService, {dataEncapsulation: false}),
ReactiveFormsModule,
@thecodinganalyst
thecodinganalyst / books.effects.ts
Created June 13, 2022 01:25
NgRx Effects Example
@Injectable()
export class BooksEffects {
loadBooks$ = createEffect(() => this.actions$.pipe(
ofType(BookStoreActions.loadBooks),
mergeMap(() => this.booksService.getBooks().pipe(
map(books => BookStoreActions.booksLoaded({books})),
catchError(() => EMPTY)
))
));
@thecodinganalyst
thecodinganalyst / books.reducer.ts
Created June 13, 2022 01:25
NgRx Reducer Example
import {createReducer, on} from "@ngrx/store";
import {BookStoreActions} from "./books.actions";
import {BookStore} from "./books.state";
export const initialState: BookStore = { books: [], selectedBook: undefined, showDetail: false};
export const booksReducer = createReducer(
initialState,
on(BookStoreActions.booksLoaded, (state, {books}) => ({...state, books: books})),
on(BookStoreActions.showBook, (state, {book}) => ({...state, selectedBook: book, showDetail: true})),