-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
🍒 Cherry picked from #3029 [feat(components/forms): file drop harness](#3029) Co-authored-by: Sandhya Raja Sabeson <[email protected]> Co-authored-by: Trevor Burch <[email protected]>
- Loading branch information
1 parent
650282b
commit b5c37ad
Showing
14 changed files
with
912 additions
and
48 deletions.
There are no files selected for viewing
12 changes: 10 additions & 2 deletions
12
apps/code-examples/src/app/code-examples/forms/file-drop/basic/demo.component.html
This file contains 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
113 changes: 113 additions & 0 deletions
113
apps/code-examples/src/app/code-examples/forms/file-drop/basic/demo.component.spec.ts
This file contains 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,113 @@ | ||
import { HarnessLoader } from '@angular/cdk/testing'; | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { FormControl } from '@angular/forms'; | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { | ||
SkyFileDropHarness, | ||
SkyFileItemHarness, | ||
provideSkyFileAttachmentTesting, | ||
} from '@skyux/forms/testing'; | ||
|
||
import { DemoComponent } from './demo.component'; | ||
|
||
describe('Basic file drop demo', () => { | ||
async function setupTest(options: { dataSkyId: string }): Promise<{ | ||
harness: SkyFileDropHarness; | ||
formControl: FormControl; | ||
loader: HarnessLoader; | ||
}> { | ||
TestBed.configureTestingModule({ | ||
providers: [provideSkyFileAttachmentTesting()], | ||
}); | ||
const fixture = TestBed.createComponent(DemoComponent); | ||
const loader = TestbedHarnessEnvironment.loader(fixture); | ||
|
||
const harness = await loader.getHarness( | ||
SkyFileDropHarness.with({ dataSkyId: options.dataSkyId }), | ||
); | ||
|
||
fixture.detectChanges(); | ||
await fixture.whenStable(); | ||
|
||
const formControl = fixture.componentInstance.fileDrop; | ||
|
||
return { harness, formControl, loader }; | ||
} | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [DemoComponent, NoopAnimationsModule], | ||
}); | ||
}); | ||
|
||
it('should set initial values', async () => { | ||
const { harness } = await setupTest({ | ||
dataSkyId: 'logo-upload', | ||
}); | ||
|
||
await expectAsync(harness.getLabelText()).toBeResolvedTo('Logo image'); | ||
await expectAsync(harness.getAcceptedTypes()).toBeResolvedTo( | ||
'image/png,image/jpeg', | ||
); | ||
await expectAsync(harness.getHintText()).toBeResolvedTo( | ||
'Upload up to 3 files under 50MB.', | ||
); | ||
await expectAsync(harness.isStacked()).toBeResolvedTo(true); | ||
|
||
await expectAsync(harness.getLinkUploadHintText()).toBeResolvedTo( | ||
'Start with http:// or https://', | ||
); | ||
}); | ||
|
||
it('should not upload invalid files starting with `a`', async () => { | ||
const { harness, formControl } = await setupTest({ | ||
dataSkyId: 'logo-upload', | ||
}); | ||
|
||
const filesToUpload: File[] = [ | ||
new File([], 'aWrongFile', { type: 'image/png' }), | ||
new File([], 'validFile', { type: 'image/png' }), | ||
]; | ||
|
||
await harness.loadFiles(filesToUpload); | ||
|
||
expect(formControl.value?.length).toBe(1); | ||
await expectAsync(harness.hasValidateFnError()).toBeResolvedTo(true); | ||
}); | ||
|
||
it('should not allow more than 3 files to be uploaded', async () => { | ||
const { harness, formControl, loader } = await setupTest({ | ||
dataSkyId: 'logo-upload', | ||
}); | ||
|
||
await harness.loadFiles([ | ||
new File([], 'validFile1', { type: 'image/png' }), | ||
new File([], 'validFile2', { type: 'image/png' }), | ||
new File([], 'validFile3', { type: 'image/png' }), | ||
]); | ||
|
||
expect(formControl.value?.length).toBe(3); | ||
await expectAsync( | ||
harness.hasCustomError('maxNumberOfFilesReached'), | ||
).toBeResolvedTo(false); | ||
expect(formControl.valid).toBe(true); | ||
|
||
await harness.enterLinkUploadText('foo.bar'); | ||
await harness.clickLinkUploadDoneButton(); | ||
|
||
expect(formControl.value?.length).toBe(4); | ||
await expectAsync( | ||
harness.hasCustomError('maxNumberOfFilesReached'), | ||
).toBeResolvedTo(true); | ||
expect(formControl.valid).toBe(false); | ||
|
||
const validFileItemHarness = await loader.getHarness( | ||
SkyFileItemHarness.with({ fileName: 'validFile2' }), | ||
); | ||
await validFileItemHarness.clickDeleteButton(); | ||
|
||
expect(formControl.value?.length).toBe(3); | ||
expect(formControl.valid).toBe(true); | ||
}); | ||
}); |
This file contains 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 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 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 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 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.