Skip to content
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

sync with master #3

Merged
merged 4 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* Created by vadimdez on 21/06/16.
*/
import { Component, ViewChild } from '@angular/core';
import { PDFProgressData, PDFDocumentProxy, PDFSource } from './pdf-viewer/pdf-viewer.module';
import {
PDFProgressData,
PDFDocumentProxy,
PDFSource
} from './pdf-viewer/pdf-viewer.module';

import { PdfViewerComponent } from './pdf-viewer/pdf-viewer.component';

Expand All @@ -12,9 +16,7 @@ import { PdfViewerComponent } from './pdf-viewer/pdf-viewer.component';
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})

export class AppComponent {

pdfSrc: string | PDFSource | ArrayBuffer = './assets/pdf-test.pdf';

// or pass options as object
Expand Down Expand Up @@ -54,7 +56,7 @@ export class AppComponent {
xhr.onload = (e: any) => {
console.log(xhr);
if (xhr.status === 200) {
const blob = new Blob([xhr.response], {type: 'application/pdf'});
const blob = new Blob([xhr.response], { type: 'application/pdf' });
this.pdfSrc = URL.createObjectURL(blob);
}
};
Expand Down Expand Up @@ -87,7 +89,7 @@ export class AppComponent {
onFileSelected() {
const $pdf: any = document.querySelector('#file');

if (typeof (FileReader) !== 'undefined') {
if (typeof FileReader !== 'undefined') {
const reader = new FileReader();

reader.onload = (e: any) => {
Expand Down Expand Up @@ -127,7 +129,9 @@ export class AppComponent {
this.error = error; // set error

if (error.name === 'PasswordException') {
const password = prompt('This document is password protected. Enter the password:');
const password = prompt(
'This document is password protected. Enter the password:'
);

if (password) {
this.error = null;
Expand All @@ -140,11 +144,11 @@ export class AppComponent {
let newSrc;

if (this.pdfSrc instanceof ArrayBuffer) {
newSrc = {data: this.pdfSrc};
newSrc = { data: this.pdfSrc };
} else if (typeof this.pdfSrc === 'string') {
newSrc = {url: this.pdfSrc};
newSrc = { url: this.pdfSrc };
} else {
newSrc = {...this.pdfSrc};
newSrc = { ...this.pdfSrc };
}

newSrc.password = password;
Expand Down
68 changes: 44 additions & 24 deletions src/app/pdf-viewer/pdf-viewer.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { PdfViewerComponent } from './pdf-viewer.component';
import { PdfViewerModule } from './pdf-viewer.module';

@Component({
template: `<pdf-viewer></pdf-viewer>`
template: `
<pdf-viewer></pdf-viewer>
`
})
class TestComponent {}

Expand All @@ -24,20 +26,17 @@ describe('AppComponent', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent
],
imports: [
PdfViewerModule
]
}).compileComponents()
.then(() => {
testFixture = TestBed.createComponent(TestComponent);
testApp = testFixture.debugElement.componentInstance;

pdfViewerFixture = TestBed.createComponent(PdfViewerComponent);
pdfViewer = pdfViewerFixture.debugElement.componentInstance;
});
declarations: [TestComponent],
imports: [PdfViewerModule]
})
.compileComponents()
.then(() => {
testFixture = TestBed.createComponent(TestComponent);
testApp = testFixture.debugElement.componentInstance;

pdfViewerFixture = TestBed.createComponent(PdfViewerComponent);
pdfViewer = pdfViewerFixture.debugElement.componentInstance;
});
}));

it('should create test component', () => {
Expand All @@ -50,7 +49,10 @@ describe('AppComponent', () => {
setPdf(10);

[1, 3, 7, 10].forEach((page: number) => {
expect((pdfViewer as any).getValidPageNumber(page)).toBe(page, `page: ${ page }`);
expect((pdfViewer as any).getValidPageNumber(page)).toBe(
page,
`page: ${page}`
);
});
});

Expand All @@ -61,16 +63,20 @@ describe('AppComponent', () => {
expect((pdfViewer as any).getValidPageNumber(pages + 2)).toBe(pages);
});

it('should return first page when page is less then 1', function () {
it('should return first page when page is less then 1', function() {
setPdf(10);
expect((pdfViewer as any).getValidPageNumber(0)).toBe(1);
expect((pdfViewer as any).getValidPageNumber(-1)).toBe(1);
});
});

describe('getScale', () => {
it('should get scale 1 with offsetWidth = 0', function () {
let spy = spyOnProperty((pdfViewer as any).element.nativeElement, 'offsetWidth', 'get').and.returnValue(0);
it('should get scale 1 with offsetWidth = 0', function() {
let spy = spyOnProperty(
(pdfViewer as any).element.nativeElement,
'offsetWidth',
'get'
).and.returnValue(0);

expect((pdfViewer as any).getScale(0)).toBe(1);
expect(spy).toHaveBeenCalled();
Expand All @@ -84,7 +90,9 @@ describe('AppComponent', () => {
it('should check default url', () => {
const PDFJS = require('pdfjs-dist/build/pdf');

expect((<any>pdfViewer)._cMapsUrl).toBe(`https://unpkg.com/pdfjs-dist@${ (PDFJS as any).version }/cmaps/`);
expect((<any>pdfViewer)._cMapsUrl).toBe(
`https://unpkg.com/pdfjs-dist@${(PDFJS as any).version}/cmaps/`
);
});

it('should return src', () => {
Expand All @@ -98,22 +106,34 @@ describe('AppComponent', () => {
pdfViewer.src = src;
pdfViewer.cMapsUrl = cMapUrl;

expect((<any>pdfViewer).getDocumentParams()).toEqual({ url: src, cMapUrl, cMapPacked: true });
expect((<any>pdfViewer).getDocumentParams()).toEqual({
url: src,
cMapUrl,
cMapPacked: true
});
});

it('should return object when src is an object', () => {
pdfViewer.src = { url: src };
pdfViewer.cMapsUrl = cMapUrl;

expect((<any>pdfViewer).getDocumentParams()).toEqual({ url: src, cMapUrl, cMapPacked: true });
expect((<any>pdfViewer).getDocumentParams()).toEqual({
url: src,
cMapUrl,
cMapPacked: true
});
});

it('should return object when src is an object with byte array', () => {
const src = new Uint8Array(1);
pdfViewer.src = { url: src as any };
pdfViewer.cMapsUrl = cMapUrl;

expect((<any>pdfViewer).getDocumentParams()).toEqual({ url: src, cMapUrl, cMapPacked: true });
expect((<any>pdfViewer).getDocumentParams()).toEqual({
url: src,
cMapUrl,
cMapPacked: true
});
});
});
});
});
Loading