Skip to content

Commit 9cf9fda

Browse files
authored
Merge pull request #7096 from StoDevX/drew/fix-building-favorites
fix redux action for favorite building hours
2 parents 55b4c1e + da68a18 commit 9cf9fda

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

modules/navigation-buttons/styles.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {StyleSheet, Platform} from 'react-native'
2+
import {link} from '@frogpond/colors'
23

34
export const commonStyles = StyleSheet.create({
45
button: {
@@ -40,6 +41,7 @@ export const rightButtonStyles = StyleSheet.create({
4041
}),
4142
},
4243
icon: {
44+
color: link,
4345
...Platform.select({
4446
ios: {
4547
fontSize: 24,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {reducer, State, toggleFavoriteBuilding} from '../parts/buildings'
2+
3+
describe('toggle favorite building hours', () => {
4+
it('should return the initial state', () => {
5+
const {favorites} = reducer(undefined, {type: undefined})
6+
expect(favorites).toEqual([])
7+
})
8+
9+
it('should handle a favorite being added to an empty list', () => {
10+
const previousState: State = {favorites: []}
11+
const {favorites} = reducer(previousState, toggleFavoriteBuilding('a'))
12+
expect(favorites).toEqual(['a'])
13+
})
14+
15+
it('should handle a favorite being added to an existing list', () => {
16+
const previousState: State = {favorites: ['a', 'b']}
17+
const {favorites} = reducer(previousState, toggleFavoriteBuilding('c'))
18+
expect(favorites).toEqual(['a', 'b', 'c'])
19+
})
20+
21+
it('should handle a favorite being removed from an existing list', () => {
22+
const previousState: State = {favorites: ['a']}
23+
const {favorites} = reducer(previousState, toggleFavoriteBuilding('a'))
24+
expect(favorites).toEqual([])
25+
})
26+
27+
it('should handle a favorite being removed from an existing list of multiple favorites', () => {
28+
const previousState: State = {favorites: ['a', 'b']}
29+
const {favorites} = reducer(previousState, toggleFavoriteBuilding('a'))
30+
expect(favorites).toEqual(['b'])
31+
})
32+
})

source/redux/parts/buildings.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {createSlice} from '@reduxjs/toolkit'
22
import type {PayloadAction} from '@reduxjs/toolkit'
33
import type {RootState} from '../store'
44

5-
type State = {
5+
export type State = {
66
favorites: Array<string>
77
}
88

@@ -17,7 +17,12 @@ const slice = createSlice({
1717
reducers: {
1818
toggleFavoriteBuilding(state, action: PayloadAction<string>) {
1919
let favoritesSet = new Set(state.favorites)
20-
favoritesSet.delete(action.payload)
20+
21+
if (favoritesSet.has(action.payload)) {
22+
favoritesSet.delete(action.payload)
23+
} else {
24+
favoritesSet.add(action.payload)
25+
}
2126

2227
let newFavorites = Array.from(favoritesSet)
2328

0 commit comments

Comments
 (0)