Repository: kiwicom/mobile
Pull request #1041 (2018-08-29 13:09:22)

MMB: Delete locally stored boarding passes


Patch:
diff --git a/app/MMB/src/scenes/tickets/TicketDeleteButton.js b/app/MMB/src/scenes/tickets/TicketDeleteButton.js
index 07684cae..5c1dc890 100644
--- a/app/MMB/src/scenes/tickets/TicketDeleteButton.js
+++ b/app/MMB/src/scenes/tickets/TicketDeleteButton.js
@@ -63,9 +63,9 @@ export class TicketDeleteButton extends React.Component<
   };
 
   deleteFilesPromise = async () => {
-    // TODO: Delete boarding passes
     try {
       await RNFetchBlob.fs.unlink(this.getEticketPath());
+      await this.deleteBoardingPasses();
       this.setState({ hasLocalFiles: false });
     } catch (error) {
       Alert.translatedAlert(null, {
@@ -75,11 +75,34 @@ export class TicketDeleteButton extends React.Component<
     }
   };
 
+  deleteBoardingPasses = async () => {
+    try {
+      const boardingPasses = await RNFetchBlob.fs.ls(
+        this.getBoardingPassPath(),
+      );
+      boardingPasses.forEach(async boardingPass => {
+        if (boardingPass.includes(`-bookingId:${this.props.bookingId}`)) {
+          await RNFetchBlob.fs.unlink(
+            `${this.getBoardingPassPath()}/${boardingPass}`,
+          );
+        }
+      });
+    } catch (error) {
+      throw error;
+    }
+  };
+
   hasLocalFiles = async () => {
-    // TODO: Check for boardingPasses
     const existsEticket = await RNFetchBlob.fs.exists(this.getEticketPath());
+    const existsBoardingPasses = await this.hasLocalBoardingPasses();
+    this.setState({ hasLocalFiles: existsEticket || existsBoardingPasses });
+  };
 
-    this.setState({ hasLocalFiles: existsEticket });
+  hasLocalBoardingPasses = async () => {
+    const boardingPasses = await RNFetchBlob.fs.ls(this.getBoardingPassPath());
+    return boardingPasses.some(file =>
+      file.includes(`-bookingId:${this.props.bookingId}`),
+    );
   };
 
   getEticketPath = () => {
@@ -89,6 +112,11 @@ export class TicketDeleteButton extends React.Component<
     return `${DocumentDir}/${eTicketPath}`;
   };
 
+  getBoardingPassPath = () => {
+    const { DocumentDir } = RNFetchBlob.fs.dirs;
+    return `${DocumentDir}/boardingPasses`;
+  };
+
   render = () => {
     if (this.state.hasLocalFiles === false) {
       return null;


Test patch:
diff --git a/app/MMB/src/scenes/tickets/__tests__/TicketDeleteButton.test.js b/app/MMB/src/scenes/tickets/__tests__/TicketDeleteButton.test.js
index 22fe081f..73e771cc 100644
--- a/app/MMB/src/scenes/tickets/__tests__/TicketDeleteButton.test.js
+++ b/app/MMB/src/scenes/tickets/__tests__/TicketDeleteButton.test.js
@@ -2,11 +2,16 @@
 
 import * as React from 'react';
 import renderer from 'react-test-renderer';
+import RNFetchBlob from 'react-native-fetch-blob';
 
 import { TicketDeleteButton } from '../TicketDeleteButton';
 
 jest.mock('react-native-fetch-blob', () => {
+  let lsReturn = [];
   return {
+    setLsReturn: input => {
+      lsReturn = input;
+    },
     config: () => ({
       fetch: async () => ({
         path: () => {},
@@ -18,6 +23,8 @@ jest.mock('react-native-fetch-blob', () => {
         DocumentDir: 'ait',
       },
       exists: async () => true,
+      unlink: async () => true,
+      ls: async () => lsReturn,
     },
   };
 });
@@ -73,4 +80,37 @@ describe('TicketDeleteButton', () => {
     Component.componentDidUpdate({ isFocused: true, bookingId: 123456 });
     expect(spy).not.toHaveBeenCalled();
   });
+
+  describe('hasLocalBoardingPasses', () => {
+    it('should return false if no files has bookingId in filename', async () => {
+      const Component = new TicketDeleteButton({
+        bookingId: 123456,
+        isFocused: true,
+      });
+
+      RNFetchBlob.setLsReturn([
+        'test-bookingId:123.pdf',
+        'lol-bookingId:321.pdf',
+      ]);
+
+      const hasLocalBoardingPasses = await Component.hasLocalBoardingPasses();
+      expect(hasLocalBoardingPasses).toEqual(false);
+    });
+
+    it('should return true if any file has bookingId in filename', async () => {
+      const Component = new TicketDeleteButton({
+        bookingId: 123456,
+        isFocused: true,
+      });
+
+      RNFetchBlob.setLsReturn([
+        'test-bookingId:123.pdf',
+        'lol-bookingId:321.pdf',
+        'lol-bookingId:123456.pdf',
+      ]);
+
+      const hasLocalBoardingPasses = await Component.hasLocalBoardingPasses();
+      expect(hasLocalBoardingPasses).toEqual(true);
+    });
+  });
 });