/* tslint:disable max-line-length */
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { HttpResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';

import { ZipConnectTestModule } from '../../../test.module';
import { PostUpdateComponent } from 'app/entities/post/post-update.component';
import { PostService } from 'app/entities/post/post.service';
import { Post } from 'app/shared/model/post.model';

describe('Component Tests', () => {
    describe('Post Management Update Component', () => {
        let comp: PostUpdateComponent;
        let fixture: ComponentFixture<PostUpdateComponent>;
        let service: PostService;

        beforeEach(() => {
            TestBed.configureTestingModule({
                imports: [ZipConnectTestModule],
                declarations: [PostUpdateComponent]
            })
                .overrideTemplate(PostUpdateComponent, '')
                .compileComponents();

            fixture = TestBed.createComponent(PostUpdateComponent);
            comp = fixture.componentInstance;
            service = fixture.debugElement.injector.get(PostService);
        });

        describe('save', () => {
            it('Should call update service on save for existing entity', fakeAsync(() => {
                // GIVEN
                const entity = new Post(123);
                spyOn(service, 'update').and.returnValue(of(new HttpResponse({ body: entity })));
                comp.post = entity;
                // WHEN
                comp.save();
                tick(); // simulate async

                // THEN
                expect(service.update).toHaveBeenCalledWith(entity);
                expect(comp.isSaving).toEqual(false);
            }));

            it('Should call create service on save for new entity', fakeAsync(() => {
                // GIVEN
                const entity = new Post();
                spyOn(service, 'create').and.returnValue(of(new HttpResponse({ body: entity })));
                comp.post = entity;
                // WHEN
                comp.save();
                tick(); // simulate async

                // THEN
                expect(service.create).toHaveBeenCalledWith(entity);
                expect(comp.isSaving).toEqual(false);
            }));
        });
    });
});
