Skip to content

Commit

Permalink
Altera forma de verificar compatilhamento
Browse files Browse the repository at this point in the history
  • Loading branch information
joao15victor08 committed Dec 11, 2023
1 parent 296462a commit 7e4da51
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
45 changes: 41 additions & 4 deletions src/app/pages/video-viewer/video-viewer.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ describe('VideoViewerComponent', () => {
}
};

navigator.canShare = () => {
{
return true;
}
}


});

Expand Down Expand Up @@ -133,21 +139,52 @@ describe('VideoViewerComponent', () => {
text: component.video.title,
url: window.location.href,
};
spyOn(navigator, 'share').and.callThrough();
spyOn(window.navigator, 'canShare').and.returnValue(true);
spyOn(navigator, 'share').and.returnValue(
new Promise((resolve, reject) => {
resolve();
})
);
component.shareVideo();
tick();
expect(navigator.share).toHaveBeenCalledWith(shareData);
}));


it('should handle unsupported share options gracefully', fakeAsync(() => {
spyOn(navigator, 'share').and.returnValue(Promise.reject(new Error('Not supported')));
spyOn(navigator, 'canShare').and.returnValue(true);
spyOn(navigator, 'share').and.returnValue(Promise.reject());
spyOn((window as any).navigator.clipboard, 'writeText').and.returnValue(Promise.resolve());
const consoleWarnSpy = spyOn(console, 'error');

component.shareVideo();
tick();
expect(consoleWarnSpy).toHaveBeenCalledWith('Erro ao compartilhar:', undefined);
}));

it('should copy video URL to clipboard if native share is not supported', fakeAsync(() => {

spyOn(navigator, 'canShare').and.returnValue(false);
// spyOnProperty(navigator, 'clipboard').and.resolveTo();
spyOn(navigator.clipboard, 'writeText').and.returnValue(Promise.resolve());
const consoleWarnSpy = spyOn(console, 'error');

component.shareVideo();
tick();
expect(consoleWarnSpy).toHaveBeenCalledWith('Erro ao compartilhar:', new Error('Not supported'));
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(window.location.href);
expect(consoleWarnSpy).not.toHaveBeenCalled();
}));

it('should handle clipboard errors gracefully', fakeAsync(() => {
spyOn(navigator, 'canShare').and.returnValue(false);
spyOn(navigator.clipboard, 'writeText').and.returnValue(Promise.reject());
const consoleWarnSpy = spyOn(console, 'error');

component.shareVideo();
tick();
expect(consoleWarnSpy).toHaveBeenCalledWith('Erro ao copiar URL:', undefined);
}));

});


});
2 changes: 1 addition & 1 deletion src/app/pages/video-viewer/video-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class VideoViewerComponent implements OnInit {
url: window.location.href,
};

if (navigator.share) {
if (navigator.canShare()) {
navigator.share(shareData)
.then(() => console.log('Compartilhado com sucesso'))
.catch((error) => console.error('Erro ao compartilhar:', error));
Expand Down

0 comments on commit 7e4da51

Please sign in to comment.