55
66import { beforeEach , describe , expect , it , vi } from 'vitest'
77import { flushPromises , mount } from '@vue/test-utils'
8- import axios from '@nextcloud/axios'
98
109import CollectMetadata from '../../../views/Settings/CollectMetadata.vue'
1110
1211const emitMock = vi . fn ( )
13-
14- vi . mock ( '@nextcloud/axios' , ( ) => ( {
15- default : {
16- get : vi . fn ( ) ,
17- } ,
12+ const fetchEffectivePoliciesMock = vi . fn ( async ( ) => { } )
13+ const saveSystemPolicyMock = vi . fn ( async ( ) => ( { policyKey : 'collect_metadata' } ) )
14+ const getEffectiveValueMock = vi . fn ( ( ) => '1' )
15+
16+ vi . mock ( '../../../store/policies' , ( ) => ( {
17+ usePoliciesStore : ( ) => ( {
18+ fetchEffectivePolicies : fetchEffectivePoliciesMock ,
19+ saveSystemPolicy : saveSystemPolicyMock ,
20+ getEffectiveValue : getEffectiveValueMock ,
21+ } ) ,
1822} ) )
1923
2024vi . mock ( '@nextcloud/event-bus' , ( ) => ( {
@@ -23,18 +27,6 @@ vi.mock('@nextcloud/event-bus', () => ({
2327
2428vi . mock ( '@nextcloud/l10n' , ( ) => globalThis . mockNextcloudL10n ( ) )
2529
26- vi . mock ( '@nextcloud/router' , ( ) => ( {
27- generateOcsUrl : vi . fn ( ( path : string ) => path ) ,
28- } ) )
29-
30- const OCP = {
31- AppConfig : {
32- setValue : vi . fn ( ) ,
33- } ,
34- }
35-
36- ; ( globalThis as typeof globalThis & { OCP : typeof OCP } ) . OCP = OCP
37-
3830describe ( 'CollectMetadata.vue' , ( ) => {
3931 beforeEach ( ( ) => {
4032 vi . clearAllMocks ( )
@@ -51,26 +43,22 @@ describe('CollectMetadata.vue', () => {
5143 } )
5244 }
5345
54- it ( 'loads enabled state from provisioning config' , async ( ) => {
55- vi . mocked ( axios . get ) . mockResolvedValue ( {
56- data : { ocs : { data : { data : '1' } } } ,
57- } )
58-
46+ it ( 'loads enabled state from effective policies' , async ( ) => {
5947 const wrapper = createWrapper ( )
6048 await flushPromises ( )
6149
50+ expect ( fetchEffectivePoliciesMock ) . toHaveBeenCalledTimes ( 1 )
51+ expect ( getEffectiveValueMock ) . toHaveBeenCalledWith ( 'collect_metadata' )
6252 expect ( wrapper . vm . collectMetadataEnabled ) . toBe ( true )
6353 } )
6454
65- it ( 'persists the flag and emits a change event on success' , ( ) => {
55+ it ( 'persists the flag and emits a change event on success' , async ( ) => {
6656 const wrapper = createWrapper ( )
6757
6858 wrapper . vm . collectMetadataEnabled = true
69- wrapper . vm . saveCollectMetadata ( )
59+ await wrapper . vm . saveCollectMetadata ( )
7060
71- expect ( OCP . AppConfig . setValue ) . toHaveBeenCalledTimes ( 1 )
72- const callbacks = vi . mocked ( OCP . AppConfig . setValue ) . mock . calls [ 0 ] [ 3 ]
73- callbacks ?. success ?.( )
61+ expect ( saveSystemPolicyMock ) . toHaveBeenCalledWith ( 'collect_metadata' , true , false )
7462
7563 expect ( emitMock ) . toHaveBeenCalledWith ( 'collect-metadata:changed' , undefined )
7664 } )
0 commit comments