Kaynağa Gözat

Fix available now logic and add tests

Tatiana Inama 6 yıl önce
ebeveyn
işleme
b2e09f0a0f

+ 21 - 12
app/src/components/Table/index.tsx

@@ -41,7 +41,7 @@ type Actions<T> = {
   }
 };
 
-const showAvailability = (months: Month[]): { color: Colors, text: string} => {
+export const showAvailability = (months: Month[]): { color: Colors, text: string} => {
   const currentMonth = (moment().get('month') as Month) + 1;
 
   if (months.length === 12){
@@ -102,10 +102,23 @@ const DisplayTime: FunctionComponent<{time: Time}> = ({ time }) => {
   );
 }
 
+export const AvailableNow = <T extends Critter>(critters: T[], currentDay: Date) => {
+  const result = critters.filter(critter => {
+    return includes(currentDay.getMonth() + 1, critter.months) && isInTimeRange(critter.time, currentDay.getHours())
+  });
+  return result;
+}
+
 const DisplayData = (type: 'time' | 'month') => type === 'time' ? DisplayTime : DisplayMonths;
 
-const isInTimeRange = (rangeTime: Time) => {
-  return rangeTime.some(([from, to]) => moment().isBetween(moment().hour(from).minute(0), moment().hour(to < from ? to + 24 : to).minute(0)));
+const isInTimeRange = (critterRangeTime: Time, currentTime: number) => {
+  return critterRangeTime.some(([from, to]) => {
+    return (from < to) ? (
+      currentTime >= from && currentTime <= to
+    ) : (
+      currentTime >= from || currentTime <= to
+    )
+  });
 }
 
 const Table = <T extends Critter>({ data, columns }: TableProps<T>) => {
@@ -136,14 +149,6 @@ const Table = <T extends Critter>({ data, columns }: TableProps<T>) => {
     })
   }, [data, columns]);
 
-  const availableNow = () => {
-    const currentMonth = moment().month() + 1;
-    const result = critters.filter(critter => {
-      return includes(currentMonth, critter.months) && isInTimeRange(critter.time)
-    });
-    setState(result)
-  }
-
   const showAll = () => {
     setState(data)
     setAction({
@@ -160,6 +165,10 @@ const Table = <T extends Critter>({ data, columns }: TableProps<T>) => {
     })
   }
 
+  const getAvailableCritters = () => {
+    const available = AvailableNow(critters, new Date());
+    setState(available)
+  }
   const search = () => {
     const { search } = actions;
     const result = critters.filter(critter => includes(search.toLowerCase(), critter.location.toLowerCase()) || includes(search.toLowerCase(), critter.name.toLowerCase()));
@@ -216,7 +225,7 @@ const Table = <T extends Critter>({ data, columns }: TableProps<T>) => {
           <Input value={actions.search} handleChange={(_filter) => { setAction({...actions, search: _filter})}}/>
           <Button onClick={() => search()}>search</Button>
         </div>
-        <Button onClick={() => { availableNow() }} color="primary">Available now</Button>
+        <Button onClick={() => { getAvailableCritters() }} color="primary">Available now</Button>
         <Button onClick={() => { showAll() }}>Show all</Button>
       </div>
       {

+ 117 - 0
app/src/components/Table/table.test.js

@@ -0,0 +1,117 @@
+import { AvailableNow } from './index';
+
+const critters = [
+  {
+    "name": "bitterling",
+    "time": [
+      [ 0, 24 ]
+    ],
+    "months": [ 1, 2, 3, 11, 12 ]
+  },
+  {
+    "name": "pale chub",
+    "time": [
+      [ 9, 16 ]
+    ],
+    "months": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
+  },
+  {
+    "name": "dace",
+    "time": [
+      [ 16, 9 ]
+    ],
+    "months": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
+  },
+  {
+    "name": "snapping turtle",
+    "time": [
+      [ 21, 4 ]
+    ],
+    "months": [ 4, 5, 6, 7, 8, 9, 10 ]
+  },
+  {
+    "name": "piranha",
+    "time": [
+      [ 9, 16 ],
+      [ 21, 4 ]
+    ],
+    "months": [ 6, 7, 8, 9 ]
+  },
+  {
+    "name": "dorado",
+    "time": [
+      [ 4, 21 ]
+    ],
+    "months": [ 6, 7, 8, 9 ]
+  }
+]
+
+describe('AvailableNow:', () => {
+  test('pale chub available on 05/05 @ 11:30hs', () => {
+    const date = new Date("2020-05-05 11:30")
+    expect(AvailableNow(critters, date)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'pale chub'})
+      ])
+    )
+  });
+
+  test('bitterling available on 09/02 @ 15:30hs', () => {
+    const date = new Date("2020-02-09 15:30")
+    expect(AvailableNow(critters, date)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'bitterling'})
+      ])
+    )
+  })
+
+  test('snapping turtle available on 10/09 @ 22:45hs', () => {
+    const date = new Date("2020-09-10 22:45")
+    expect(AvailableNow(critters, date)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'snapping turtle'})
+      ])
+    )
+  })
+
+  test('dace available on 10/09 @ 7:15hs', () => {
+    const date = new Date("2020-09-10 7:15")
+    expect(AvailableNow(critters, date)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'dace'})
+      ])
+    )
+  })
+
+  test('piranha available on 20/06 @ 13:00hs and 03:52hs', () => {
+    const date1 = new Date("2020-06-20 13:00")
+    expect(AvailableNow(critters, date1)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'piranha'})
+      ])
+    )
+    
+    const date2 = new Date("2020-06-20 @ 03:52")
+    expect(AvailableNow(critters, date2)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'piranha'})
+      ])
+    )
+  })
+
+  test('dorado available on 17/7 @ 20hs but not on april', () => {
+    const date = new Date("2020-07-17 20:00")
+    expect(AvailableNow(critters, date)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'dorado'})
+      ])
+    )
+
+    const date2 = new Date("2020-03-17 20:00")
+    expect(AvailableNow(critters, date)).toEqual(
+      expect.arrayContaining([
+        expect.objectContaining({name: 'dorado'})
+      ])
+    )
+  })
+})