Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
refactor: set currentPage / currentPerPage only if different from cur…
Browse files Browse the repository at this point in the history
…rent value (#1612)

* refactor: set currentPage / currentPerPage only if different from current value

* tests: add unit tests

* fix: reset page when perpage update

* test: adjust test for reset currentPage

Co-authored-by: Davi Figueiredo <[email protected]>
Co-authored-by: Alex Barnsley <[email protected]>
  • Loading branch information
3 people authored Feb 24, 2020
1 parent e2a3320 commit f6b4126
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const createWrapper = (component, gettersTransactions) => {
getters: {
get 'transaction/byAddress' () {
return gettersTransactions
}
},
'session/transactionTableRowCount': 10
}
},
session_profile: {
Expand Down Expand Up @@ -532,5 +533,64 @@ describe('WalletTransactions', () => {
expect(wrapper.vm.newTransactionsNotice).toBe('test')
})
})

describe('onPageChange', () => {
it('should do nothing if currentPage has not changed', () => {
const spyParams = jest.spyOn(WalletTransactionsMixin.methods, '__updateParams').mockImplementation()
const spyTransactions = jest.spyOn(WalletTransactionsMixin.methods, 'loadTransactions').mockImplementation()
createWrapper()

wrapper.vm.onPageChange({ currentPage: 1 })
expect(spyParams).not.toHaveBeenCalled()
expect(spyTransactions).toHaveBeenCalledTimes(1) // gets called once in the created lifecycle hook

spyParams.mockRestore()
spyTransactions.mockRestore()
})

it('should update params and load transactions if currentPage has changed', () => {
const spyParams = jest.spyOn(WalletTransactionsMixin.methods, '__updateParams').mockImplementation()
const spyTransactions = jest.spyOn(WalletTransactionsMixin.methods, 'loadTransactions').mockImplementation()
createWrapper()

wrapper.vm.onPageChange({ currentPage: 10 })
expect(wrapper.vm.currentPage).toBe(10)
expect(spyParams).toHaveBeenCalledTimes(1)
expect(spyTransactions).toHaveBeenCalledTimes(2) // gets called once in the created lifecycle hook

spyParams.mockRestore()
spyTransactions.mockRestore()
})
})

describe('onPerPageChange', () => {
it('should do nothing if currentPerPage has not changed', () => {
const spyParams = jest.spyOn(WalletTransactionsMixin.methods, '__updateParams').mockImplementation()
const spyTransactions = jest.spyOn(WalletTransactionsMixin.methods, 'loadTransactions').mockImplementation()
createWrapper()

wrapper.vm.onPerPageChange({ currentPerPage: 10 })
expect(spyParams).not.toHaveBeenCalled()
expect(spyTransactions).toHaveBeenCalledTimes(1) // gets called once in the created lifecycle hook

spyParams.mockRestore()
spyTransactions.mockRestore()
})

it('should update params and load transactions if currentPerPage has changed', () => {
const spyParams = jest.spyOn(WalletTransactionsMixin.methods, '__updateParams').mockImplementation()
const spyTransactions = jest.spyOn(WalletTransactionsMixin.methods, 'loadTransactions').mockImplementation()
createWrapper()

wrapper.vm.currentPage = 5

wrapper.vm.onPerPageChange({ currentPerPage: 20 })
expect(spyParams).toHaveBeenCalledTimes(1)
expect(spyTransactions).toHaveBeenCalledTimes(2) // gets called once in the created lifecycle hook
expect(wrapper.vm.currentPage).toBe(1)
spyParams.mockRestore()
spyTransactions.mockRestore()
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,20 @@ export default {
},

onPageChange ({ currentPage }) {
if (!currentPage) {
return
if (currentPage && currentPage !== this.currentPage) {
this.currentPage = currentPage
this.__updateParams({ page: currentPage })
this.loadTransactions()
}

this.currentPage = currentPage
this.__updateParams({ page: currentPage })
this.loadTransactions()
},

onPerPageChange ({ currentPerPage }) {
if (!currentPerPage) {
return
if (currentPerPage && currentPerPage !== this.transactionTableRowCount) {
this.transactionTableRowCount = currentPerPage
this.currentPage = 1
this.__updateParams({ limit: currentPerPage, page: 1 })
this.loadTransactions()
}

this.__updateParams({ limit: currentPerPage, page: 1 })
this.loadTransactions()
this.transactionTableRowCount = currentPerPage
},

onSortChange ({ source, field, type }) {
Expand Down

0 comments on commit f6b4126

Please sign in to comment.