Skip to content

Commit

Permalink
fix desktop tets, saga and currentChannelAddress selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper-RF committed May 17, 2023
1 parent 6d698a7 commit a69ab29
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { renderComponent } from '../../../testUtils/renderComponent'
import { getFactory, publicChannels, communities, identity } from '@quiet/state-manager'
import ChannelsPanel from './ChannelsPanel'
import { DateTime } from 'luxon'
import { generateChannelAddress } from '@quiet/common'

describe('Channels panel', () => {
let socket: MockedSocket
Expand All @@ -26,7 +27,7 @@ describe('Channels panel', () => {
const community = await factory.create<
ReturnType<typeof communities.actions.addNewCommunity>['payload']
>('Community')

const generalChannel = publicChannels.selectors.generalChannel(store.getState())
const alice = await factory.create<
ReturnType<typeof identity.actions.addNewIdentity>['payload']
>('Identity', { id: community.id, nickname: 'alice' })
Expand All @@ -43,7 +44,7 @@ describe('Channels panel', () => {
description: `Welcome to #${name}`,
timestamp: DateTime.utc().valueOf(),
owner: alice.nickname,
address: name
address: generateChannelAddress(name)
}
}
)
Expand All @@ -56,7 +57,7 @@ describe('Channels panel', () => {
channels={channels}
unreadChannels={[]}
setCurrentChannel={function (_address: string): void {}}
currentChannel={'general'}
currentChannelAddress={generalChannel.address}
createChannelModal={{
open: false,
handleOpen: function (_args?: any): any {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export interface ChannelsPanelProps {
channels: PublicChannel[]
unreadChannels: string[]
setCurrentChannel: (address: string) => void
currentChannel: string
currentChannelAddress: string
createChannelModal: ReturnType<typeof useModal>
}

const ChannelsPanel: React.FC<ChannelsPanelProps> = ({
channels,
unreadChannels,
setCurrentChannel,
currentChannel,
currentChannelAddress,
createChannelModal
}) => {
return (
Expand All @@ -35,7 +35,7 @@ const ChannelsPanel: React.FC<ChannelsPanelProps> = ({
<List disablePadding data-testid='channelsList'>
{channels.map((channel, index) => {
const unread = unreadChannels.some(address => address === channel.address)
const selected = currentChannel === channel.address
const selected = currentChannelAddress === channel.address
return (
<ChannelsListItem
channel={channel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { ioMock } from '../../../shared/setupTests'
import { prepareStore } from '../../testUtils/prepareStore'
import { setupCrypto } from '@quiet/identity'
import { call, fork } from 'typed-redux-saga'
import { publicChannels, NotificationsSounds, MessageType, FileMetadata } from '@quiet/state-manager'
import {
publicChannels,
NotificationsSounds,
MessageType,
FileMetadata
} from '@quiet/state-manager'
import {
createNotification,
handleNotificationActions,
NotificationData
} from './notifications.saga'
import { generateChannelAddress } from '@quiet/common'

const notification = jest.fn().mockImplementation(() => {
return jest.fn()
Expand Down Expand Up @@ -47,26 +53,29 @@ describe('clicking in notification', () => {

const { store, runSaga } = await prepareStore({}, socket)

const channel = 'sailing'
const generalAddress = generateChannelAddress('general')
const sailingAddress = generateChannelAddress('sailing')

const notificationData: NotificationData = {
label: 'label',
body: 'body',
channel: channel,
channel: sailingAddress,
sound: NotificationsSounds.splat
}

store.dispatch(publicChannels.actions.setCurrentChannel({ channelAddress: generalAddress }))

// Verify current channel is 'general
expect(publicChannels.selectors.currentChannelAddress(store.getState())).toBe('general')
expect(publicChannels.selectors.currentChannelAddress(store.getState())).toBe(generalAddress)

runSaga(function* (): Generator {
const notification = yield* call(createNotification, notificationData)
yield* fork(handleNotificationActions, notification, MessageType.Basic, channel)
yield* fork(handleNotificationActions, notification, MessageType.Basic, sailingAddress)
yield* call(notification.onclick, new Event(''))
})

// Confirm current channel address has changed
expect(publicChannels.selectors.currentChannelAddress(store.getState())).toBe(channel)
expect(publicChannels.selectors.currentChannelAddress(store.getState())).toBe(sailingAddress)
})

it('opens file explorer', async () => {
Expand All @@ -75,7 +84,7 @@ describe('clicking in notification', () => {

const { runSaga } = await prepareStore({}, socket)

const channel = 'sailing'
const sailingAddress = generateChannelAddress('sailing')

const media: FileMetadata = {
cid: 'cid',
Expand All @@ -84,22 +93,22 @@ describe('clicking in notification', () => {
path: 'path/file.ext',
message: {
id: 'id',
channelAddress: channel
channelAddress: sailingAddress
}
}

const notificationData: NotificationData = {
label: 'label',
body: 'body',
channel: channel,
channel: sailingAddress,
sound: NotificationsSounds.splat
}

const spy = jest.spyOn(shell, 'showItemInFolder')

runSaga(function* (): Generator {
const notification = yield* call(createNotification, notificationData)
yield* fork(handleNotificationActions, notification, MessageType.File, channel, media)
yield* fork(handleNotificationActions, notification, MessageType.File, sailingAddress, media)
yield* call(notification.onclick, new Event(''))
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function* displayMessageNotificationSaga(
): Generator {
const incomingMessages = action.payload.messages

const currentChannel = yield* select(publicChannels.selectors.currentChannel)
const currentChannelAddress = yield* select(publicChannels.selectors.currentChannelAddress)

const currentIdentity = yield* select(identity.selectors.currentIdentity)
const certificatesMapping = yield* select(users.selectors.certificatesMapping)

Expand All @@ -50,7 +51,7 @@ export function* displayMessageNotificationSaga(
const focused = yield* call(isWindowFocused)

// Do not display notifications for active channel (when the app is in foreground)
if (focused && message.channelAddress === currentChannel.address) return
if (focused && message.channelAddress === currentChannelAddress) return

// Do not display notifications for own messages
const sender = certificatesMapping[message.pubKey]?.username
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ beforeAll(async () => {
community = await factory.create<
ReturnType<typeof communities.actions.addNewCommunity>['payload']
>('Community')

const generalChannel = publicChannels.selectors.generalChannel(store.getState())
store.dispatch(publicChannels.actions.setCurrentChannel({ channelAddress: generalChannel.address }))
sailingChannel = (
await factory.create<ReturnType<typeof publicChannels.actions.addChannel>['payload']>(
'PublicChannel'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { formatMessageDisplayDay } from '../../utils/functions/dates/formatMessa
import { displayableMessage } from '../../utils/functions/dates/formatDisplayableMessage'
import {
DisplayableMessage,
INITIAL_CURRENT_CHANNEL_ADDRESS,
MessagesDailyGroups,
PublicChannel,
PublicChannelStatus
Expand Down Expand Up @@ -86,7 +87,7 @@ export const sortedChannels = createSelector(publicChannels, (channels) => {
return sorted
})

export const currentChannelAddress = createSelector(
export const currentChannelAddress_OLD = createSelector(
selectState,
(state) => {
if (!state) return undefined
Expand All @@ -98,6 +99,26 @@ export const generalChannel = createSelector(publicChannels, publicChannelsSelec
return publicChannelsSelector.find(channel => channel.name === 'general')
})

export const currentChannelAddress = createSelector(
selectState,
generalChannel,
(state, general) => {
// KACPER - test for it - IMPORTANT !!!!
if (!state) {
return undefined
}
if (state.currentChannelAddress === INITIAL_CURRENT_CHANNEL_ADDRESS) {
if (general) {
return general.address
} else {
return 'general'// case for tests when is no channel in store
}
} else {
return state.currentChannelAddress
}
}
)

export const recentChannels = createSelector(
publicChannels,
generalChannel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ import {
ChannelDeletionResponsePayload,
DeleteChannelFromStorePayload,
ClearMessagesCachePayload,
DisableChannelPayload
DisableChannelPayload,
INITIAL_CURRENT_CHANNEL_ADDRESS
} from './publicChannels.types'

import logger from '../../utils/logger'
import { Identity } from '../identity/identity.types'
const log = logger('publicChannels')

export class PublicChannelsState {
// KACPER
public currentChannelAddress: string = ''
public currentChannelAddress: string = INITIAL_CURRENT_CHANNEL_ADDRESS

public pendingGeneralChannelRecreation: boolean = false

Expand Down Expand Up @@ -152,6 +152,7 @@ export const publicChannelsSlice = createSlice({
}>
) => {
const { message } = action.payload
console.log({ message })
channelMessagesAdapter.addOne(
state.channels.entities[message.channelAddress].messages,
message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Dictionary, EntityState } from '@reduxjs/toolkit'
import { FileMetadata } from '../files/files.types'

export const INITIAL_CURRENT_CHANNEL_ADDRESS = 'initialCurrentChannelAddress'

export interface PublicChannel {
name: string
description: string
Expand Down
42 changes: 30 additions & 12 deletions packages/state-manager/src/utils/tests/factories.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import factoryGirl from 'factory-girl'
import { CustomReduxAdapter } from './reduxAdapter'
import { Store } from '../../sagas/store.types'
import { communities, identity, messages, publicChannels, users, errors, DownloadState } from '../..'
import {
createMessageSignatureTestHelper,
createPeerIdTestHelper
} from './helpers'
communities,
identity,
messages,
publicChannels,
users,
errors,
DownloadState
} from '../..'
import { createMessageSignatureTestHelper, createPeerIdTestHelper } from './helpers'
import { getCrypto } from 'pkijs'
import { stringToArrayBuffer } from 'pvutils'
import { createRootCertificateTestHelper, createUserCertificateTestHelper, keyObjectFromString, verifySignature } from '@quiet/identity'
import {
createRootCertificateTestHelper,
createUserCertificateTestHelper,
keyObjectFromString,
verifySignature
} from '@quiet/identity'
import { MessageType, SendingStatus } from '../../sagas/messages/messages.types'
import { DateTime } from 'luxon'
import { messagesActions } from '../../sagas/messages/messages.slice'
Expand Down Expand Up @@ -46,7 +56,7 @@ export const getFactory = async (store: Store) => {
store.dispatch(communities.actions.setCurrentCommunity(payload.id))
}
// Create 'general' channel
await factory.create('PublicChannel', {
await factory.create('PublicChannel', {
communityId: payload.id,
channel: {
name: 'general',
Expand Down Expand Up @@ -136,15 +146,21 @@ export const getFactory = async (store: Store) => {
description: 'Description',
timestamp: DateTime.utc().toSeconds(),
owner: factory.assoc('Identity', 'nickname'),
address: generateChannelAddress(factory.sequence('PublicChannel.name', n => `publicChannel${n}`).toString())
address: generateChannelAddress(
factory.sequence('PublicChannel.name', n => `publicChannel${n}`).toString()
)
}
},
{
afterCreate: async (
payload: ReturnType<typeof publicChannels.actions.addChannel>['payload']
) => {
await factory.create('PublicChannelsMessagesBase', ({ channelAddress: payload.channel.address }))
await factory.create('PublicChannelSubscription', ({ channelAddress: payload.channel.address }))
await factory.create('PublicChannelsMessagesBase', {
channelAddress: payload.channel.address
})
await factory.create('PublicChannelSubscription', {
channelAddress: payload.channel.address
})
return payload
}
}
Expand Down Expand Up @@ -210,9 +226,11 @@ export const getFactory = async (store: Store) => {
afterCreate: async (
payload: ReturnType<typeof publicChannels.actions.test_message>['payload']
) => {
store.dispatch(messagesActions.incomingMessages({
messages: [payload.message]
}))
store.dispatch(
messagesActions.incomingMessages({
messages: [payload.message]
})
)

return payload
}
Expand Down

0 comments on commit a69ab29

Please sign in to comment.