subtitles.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { convertSrtFileToString, convertSrtStringToVttString, convertSrtToVtt } from './subtitles'
  2. const srtTextExample = `
  3. 1
  4. 00:05:00,400 --> 00:05:15,300
  5. This is an example of a subtitle.
  6. This is second line of a subtitle.
  7. 2
  8. 00:05:16,400 --> 00:05:25,300
  9. This is an example of a subtitle - 2nd subtitle.
  10. `
  11. const vttTextExample = `WEBVTT
  12. 1
  13. 00:05:00.400 --> 00:05:15.300
  14. This is an example of a subtitle.
  15. This is second line of a subtitle.
  16. 2
  17. 00:05:16.400 --> 00:05:25.300
  18. This is an example of a subtitle - 2nd subtitle.
  19. `
  20. .split('\n')
  21. .join('\r\n')
  22. const srtBlob = new Blob([srtTextExample], { type: 'text/plain' })
  23. const srtFile = new File([srtBlob], 'file.srt')
  24. const pdfFile = new File([srtBlob], 'file.pdf')
  25. const vttBlob = new Blob([vttTextExample])
  26. const vttFile = new File([vttBlob], 'file.vtt', { type: 'text/vtt' })
  27. describe('convertSrtFileToString', () => {
  28. it('should return a promise', () => {
  29. expect(convertSrtFileToString(srtFile)).toBeInstanceOf(Promise)
  30. })
  31. it('should convert srt file to string once resolved', async () => {
  32. const result = await convertSrtFileToString(srtFile)
  33. expect(typeof result).toEqual('string')
  34. })
  35. it('should throw error for file with wrong format', async () => {
  36. await expect(convertSrtFileToString(pdfFile)).rejects.toThrowError('File is not srt file')
  37. })
  38. })
  39. describe('convertSrtStringToVttString', () => {
  40. it('should add leading text at the top of the file', () => {
  41. expect(convertSrtStringToVttString(srtTextExample).split('\r\n')[0]).toEqual('WEBVTT')
  42. })
  43. it('should correctly convert subtitles to vtt format', () => {
  44. expect(convertSrtStringToVttString(srtTextExample)).toEqual(vttTextExample)
  45. })
  46. })
  47. describe('convertSrtToVtt', () => {
  48. it('should return a promise', () => {
  49. expect(convertSrtToVtt(srtFile)).toBeInstanceOf(Promise)
  50. })
  51. it('should have the same file name, but different extension', async () => {
  52. expect((await convertSrtToVtt(srtFile)).name).toBe(vttFile.name)
  53. })
  54. it('should have a type text/vtt', async () => {
  55. expect((await convertSrtToVtt(srtFile)).type).toBe(vttFile.type)
  56. })
  57. it('should return instance of file once resolved', async () => {
  58. expect(await convertSrtToVtt(srtFile)).toBeInstanceOf(File)
  59. })
  60. })