Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Add accepted types attribute to file input (#658)
Browse files Browse the repository at this point in the history
* Add accepted types attribute to file input

* Handle no file type and add tests

* Up mismatch percentage and add more helpful logging when screenshots fail

* Update mismatch tolerance
  • Loading branch information
Blackbaud-PatrickOFriel authored May 4, 2017
1 parent 547a10a commit 5e5af44
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
6 changes: 5 additions & 1 deletion config/utils/visual-browser-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

function checkVisualResult(results, options, browser) {
results.forEach(function (element) {
if (!element.isWithinMisMatchTolerance) {
log('Screenshot has mismatch percentage of ' + element.misMatchPercentage);
}

expect(element.isWithinMisMatchTolerance).toBe(true);
});

Expand Down Expand Up @@ -127,7 +131,7 @@
referenceName: getScreenshotName(path.join(process.cwd(), referenceFolder)),
screenshotName: getScreenshotName(path.join(process.cwd(), screenshotFolder)),
diffName: getScreenshotName(path.join(process.cwd(), diffsFolder)),
misMatchTolerance: 0.01
misMatchTolerance: 1.5
}),
viewportChangePause: 300
};
Expand Down
1 change: 1 addition & 0 deletions src/modules/fileattachments/file-drop.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
type="file"
class="sky-file-input-hidden"
[attr.multiple]="multiple ? multiple: null"
[attr.accept]="acceptedTypes ? acceptedTypes: null"
(change)="fileChangeEvent($event)"
#fileInput/>
<div class="sky-file-drop-contents" *ngIf="customEl.children.length === 0">
Expand Down
75 changes: 62 additions & 13 deletions src/modules/fileattachments/file-drop.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,24 @@ describe('File drop component', () => {
};
}

function setupStandardFileChangeEvent() {
function setupStandardFileChangeEvent(files?: Array<any>) {
let fileReaderSpy = setupFileReaderSpy();

triggerChangeEvent([
{
name: 'foo.txt',
size: 1000,
type: 'image/png'
},
{
name: 'woo.txt',
size: 2000,
type: 'image/jpeg'
}
]);
if (!files) {
files = [
{
name: 'foo.txt',
size: 1000,
type: 'image/png'
},
{
name: 'woo.txt',
size: 2000,
type: 'image/jpeg'
}
];
}
triggerChangeEvent(files);

fixture.detectChanges();

Expand Down Expand Up @@ -283,6 +286,15 @@ describe('File drop component', () => {
expect(inputEl.nativeElement.hasAttribute('multiple')).toBe(true);
});

it('should set accepted types on the file input html', () => {
componentInstance.acceptedTypes = 'image/png';
fixture.detectChanges();
let inputEl = getInputDebugEl();

expect(inputEl.nativeElement.getAttribute('accept')).toBe('image/png');

});

it('should allow the user to specify a min file size', () => {
let filesChangedActual: SkyFileDropChange;

Expand Down Expand Up @@ -383,6 +395,43 @@ describe('File drop component', () => {
expect(filesChangedActual.files[0].file.size).toBe(1000);
});

it('should reject a file with no type when accepted types are defined', () => {
let filesChangedActual: SkyFileDropChange;

componentInstance.filesChanged.subscribe(
(filesChanged: SkyFileDropChange) => filesChangedActual = filesChanged );

componentInstance.acceptedTypes = 'image/png,image/tiff';

fixture.detectChanges();

let files = [
{
name: 'foo.txt',
size: 1000
},
{
name: 'woo.txt',
size: 2000,
type: 'image/jpeg'
}
];

setupStandardFileChangeEvent(files);

expect(filesChangedActual.rejectedFiles.length).toBe(2);
expect(filesChangedActual.rejectedFiles[1].file.name).toBe('woo.txt');
expect(filesChangedActual.rejectedFiles[1].file.size).toBe(2000);
expect(filesChangedActual.rejectedFiles[1].errorType).toBe('fileType');
expect(filesChangedActual.rejectedFiles[1].errorParam).toBe(componentInstance.acceptedTypes);

expect(filesChangedActual.rejectedFiles[0].file.name).toBe('foo.txt');
expect(filesChangedActual.rejectedFiles[0].file.size).toBe(1000);
expect(filesChangedActual.rejectedFiles[0].errorType).toBe('fileType');
expect(filesChangedActual.rejectedFiles[0].errorParam).toBe(componentInstance.acceptedTypes);

});

it('should allow the user to specify accepted type with wildcards', () => {
let filesChangedActual: SkyFileDropChange;

Expand Down
6 changes: 5 additions & 1 deletion src/modules/fileattachments/file-drop.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export class SkyFileDropComponent {
return false;
}

if (!fileType) {
return true;
}

let acceptedTypesUpper = this.acceptedTypes.toUpperCase();
let typeArray = acceptedTypesUpper.split(',');
return !this.fileTypeInArray(typeArray, fileType.toUpperCase());
Expand All @@ -244,7 +248,7 @@ export class SkyFileDropComponent {
fileItem.errorType = 'maxFileSize';
fileItem.errorParam = this.maxFileSize.toString();
this.filesRejected(fileItem, validFileArray, rejectedFileArray, totalFiles);
} else if (fileItem.file.type && this.fileTypeRejected(fileItem.file.type)) {
} else if (this.fileTypeRejected(fileItem.file.type)) {
fileItem.errorType = 'fileType';
fileItem.errorParam = this.acceptedTypes;
this.filesRejected(fileItem, validFileArray, rejectedFileArray, totalFiles);
Expand Down

0 comments on commit 5e5af44

Please sign in to comment.