import-parser.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as fs from 'fs-extra';
  2. import * as path from 'path';
  3. import { ImportParser } from './import-parser';
  4. describe('ImportParser', () => {
  5. describe('parseProducts', () => {
  6. it('single product with a single variant', async () => {
  7. const importParser = new ImportParser();
  8. const input = await loadTestFixture('single-product-single-variant.csv');
  9. const result = await importParser.parseProducts(input);
  10. expect(result.results).toMatchSnapshot();
  11. });
  12. it('single product with a multiple variants', async () => {
  13. const importParser = new ImportParser();
  14. const input = await loadTestFixture('single-product-multiple-variants.csv');
  15. const result = await importParser.parseProducts(input);
  16. expect(result.results).toMatchSnapshot();
  17. });
  18. it('multiple products with multiple variants', async () => {
  19. const importParser = new ImportParser();
  20. const input = await loadTestFixture('multiple-products-multiple-variants.csv');
  21. const result = await importParser.parseProducts(input);
  22. expect(result.results).toMatchSnapshot();
  23. });
  24. it('works with streamed input', async () => {
  25. const importParser = new ImportParser();
  26. const filename = path.join(__dirname, 'test-fixtures', 'multiple-products-multiple-variants.csv');
  27. const input = fs.createReadStream(filename);
  28. const result = await importParser.parseProducts(input);
  29. expect(result.results).toMatchSnapshot();
  30. });
  31. describe('error conditions', () => {
  32. it('reports errors on invalid option values', async () => {
  33. const importParser = new ImportParser();
  34. const input = await loadTestFixture('invalid-option-values.csv');
  35. const result = await importParser.parseProducts(input);
  36. expect(result.errors).toEqual([
  37. 'The number of optionValues must match the number of optionGroups on line 2',
  38. 'The number of optionValues must match the number of optionGroups on line 3',
  39. 'The number of optionValues must match the number of optionGroups on line 4',
  40. 'The number of optionValues must match the number of optionGroups on line 5',
  41. ]);
  42. });
  43. it('reports error on ivalid columns', async () => {
  44. const importParser = new ImportParser();
  45. const input = await loadTestFixture('invalid-columns.csv');
  46. const result = await importParser.parseProducts(input);
  47. expect(result.results).toEqual([]);
  48. expect(result.errors).toEqual([
  49. 'The import file is missing the following columns: "slug", "assets"',
  50. ]);
  51. });
  52. it('reports error on ivalid row length', async () => {
  53. const importParser = new ImportParser();
  54. const input = await loadTestFixture('invalid-row-length.csv');
  55. const result = await importParser.parseProducts(input);
  56. expect(result.errors).toEqual([
  57. 'Invalid Record Length: header length is 10, got 8 on line 3',
  58. 'Invalid Record Length: header length is 10, got 1 on line 4',
  59. ]);
  60. expect(result.results.length).toBe(2);
  61. });
  62. });
  63. });
  64. });
  65. function loadTestFixture(fileName: string): Promise<string> {
  66. return fs.readFile(path.join(__dirname, 'test-fixtures', fileName), 'utf-8');
  67. }