@moontra/moonui 2.2.7 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +151 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +151 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/ui/locked-component.tsx +219 -23
- package/src/components/ui/pro-component-wrapper.tsx +261 -0
- package/src/__tests__/use-pro-access.test.tsx +0 -183
- package/src/use-subscription.ts +0 -37
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import { renderHook } from '@testing-library/react'
|
|
2
|
-
import { useSession } from 'next-auth/react'
|
|
3
|
-
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
4
|
-
import { useProAccess } from '../use-pro-access'
|
|
5
|
-
import React from 'react'
|
|
6
|
-
|
|
7
|
-
// Mock next-auth
|
|
8
|
-
jest.mock('next-auth/react')
|
|
9
|
-
const mockUseSession = useSession as jest.MockedFunction<typeof useSession>
|
|
10
|
-
|
|
11
|
-
// Mock fetch
|
|
12
|
-
global.fetch = jest.fn()
|
|
13
|
-
|
|
14
|
-
// Test wrapper with QueryClient
|
|
15
|
-
const createWrapper = () => {
|
|
16
|
-
const queryClient = new QueryClient({
|
|
17
|
-
defaultOptions: {
|
|
18
|
-
queries: {
|
|
19
|
-
retry: false,
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
})
|
|
23
|
-
const TestWrapper = ({ children }: { children: React.ReactNode }) => (
|
|
24
|
-
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
25
|
-
)
|
|
26
|
-
TestWrapper.displayName = 'TestWrapper'
|
|
27
|
-
return TestWrapper
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
describe('useProAccess Hook', () => {
|
|
31
|
-
beforeEach(() => {
|
|
32
|
-
jest.clearAllMocks()
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
it('returns false for unauthenticated users', () => {
|
|
36
|
-
mockUseSession.mockReturnValue({
|
|
37
|
-
data: null,
|
|
38
|
-
status: 'unauthenticated',
|
|
39
|
-
update: jest.fn(),
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
const { result } = renderHook(() => useProAccess(), {
|
|
43
|
-
wrapper: createWrapper()
|
|
44
|
-
})
|
|
45
|
-
expect(result.current.hasProAccess).toBe(false)
|
|
46
|
-
expect(result.current.isLoading).toBe(false)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('returns false for authenticated users without pro subscription', () => {
|
|
50
|
-
mockUseSession.mockReturnValue({
|
|
51
|
-
data: {
|
|
52
|
-
user: {
|
|
53
|
-
id: '1',
|
|
54
|
-
email: 'user@example.com',
|
|
55
|
-
name: 'Test User',
|
|
56
|
-
},
|
|
57
|
-
expires: '2024-12-31',
|
|
58
|
-
},
|
|
59
|
-
status: 'authenticated',
|
|
60
|
-
update: jest.fn(),
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
const { result } = renderHook(() => useProAccess(), {
|
|
64
|
-
wrapper: createWrapper()
|
|
65
|
-
})
|
|
66
|
-
expect(result.current.hasProAccess).toBe(false)
|
|
67
|
-
expect(result.current.isLoading).toBe(false)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('returns true for authenticated users with active pro subscription', () => {
|
|
71
|
-
mockUseSession.mockReturnValue({
|
|
72
|
-
data: {
|
|
73
|
-
user: {
|
|
74
|
-
id: '1',
|
|
75
|
-
email: 'pro@example.com',
|
|
76
|
-
name: 'Pro User',
|
|
77
|
-
subscription: {
|
|
78
|
-
plan: 'pro',
|
|
79
|
-
status: 'active',
|
|
80
|
-
expiresAt: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(), // 30 days from now
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
expires: '2024-12-31',
|
|
84
|
-
},
|
|
85
|
-
status: 'authenticated',
|
|
86
|
-
update: jest.fn(),
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
const { result } = renderHook(() => useProAccess(), {
|
|
90
|
-
wrapper: createWrapper()
|
|
91
|
-
})
|
|
92
|
-
expect(result.current.hasProAccess).toBe(true)
|
|
93
|
-
expect(result.current.isLoading).toBe(false)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
it('returns false for users with expired pro subscription', () => {
|
|
97
|
-
mockUseSession.mockReturnValue({
|
|
98
|
-
data: {
|
|
99
|
-
user: {
|
|
100
|
-
id: '1',
|
|
101
|
-
email: 'expired@example.com',
|
|
102
|
-
name: 'Expired User',
|
|
103
|
-
subscription: {
|
|
104
|
-
plan: 'pro',
|
|
105
|
-
status: 'active',
|
|
106
|
-
expiresAt: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), // 1 day ago
|
|
107
|
-
},
|
|
108
|
-
},
|
|
109
|
-
expires: '2024-12-31',
|
|
110
|
-
},
|
|
111
|
-
status: 'authenticated',
|
|
112
|
-
update: jest.fn(),
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
const { result } = renderHook(() => useProAccess(), {
|
|
116
|
-
wrapper: createWrapper()
|
|
117
|
-
})
|
|
118
|
-
expect(result.current.hasProAccess).toBe(false)
|
|
119
|
-
expect(result.current.isLoading).toBe(false)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
it('returns false for users with cancelled pro subscription', () => {
|
|
123
|
-
mockUseSession.mockReturnValue({
|
|
124
|
-
data: {
|
|
125
|
-
user: {
|
|
126
|
-
id: '1',
|
|
127
|
-
email: 'cancelled@example.com',
|
|
128
|
-
name: 'Cancelled User',
|
|
129
|
-
subscription: {
|
|
130
|
-
plan: 'pro',
|
|
131
|
-
status: 'cancelled',
|
|
132
|
-
expiresAt: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(),
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
expires: '2024-12-31',
|
|
136
|
-
},
|
|
137
|
-
status: 'authenticated',
|
|
138
|
-
update: jest.fn(),
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
const { result } = renderHook(() => useProAccess(), {
|
|
142
|
-
wrapper: createWrapper()
|
|
143
|
-
})
|
|
144
|
-
expect(result.current.hasProAccess).toBe(false)
|
|
145
|
-
expect(result.current.isLoading).toBe(false)
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
it('returns loading state when session is loading', () => {
|
|
149
|
-
mockUseSession.mockReturnValue({
|
|
150
|
-
data: null,
|
|
151
|
-
status: 'loading',
|
|
152
|
-
update: jest.fn(),
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
const { result } = renderHook(() => useProAccess(), {
|
|
156
|
-
wrapper: createWrapper()
|
|
157
|
-
})
|
|
158
|
-
expect(result.current.hasProAccess).toBe(false)
|
|
159
|
-
expect(result.current.isLoading).toBe(true)
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
it('handles edge cases with missing subscription data', () => {
|
|
163
|
-
mockUseSession.mockReturnValue({
|
|
164
|
-
data: {
|
|
165
|
-
user: {
|
|
166
|
-
id: '1',
|
|
167
|
-
email: 'user@example.com',
|
|
168
|
-
name: 'Test User',
|
|
169
|
-
subscription: undefined,
|
|
170
|
-
},
|
|
171
|
-
expires: '2024-12-31',
|
|
172
|
-
},
|
|
173
|
-
status: 'authenticated',
|
|
174
|
-
update: jest.fn(),
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
const { result } = renderHook(() => useProAccess(), {
|
|
178
|
-
wrapper: createWrapper()
|
|
179
|
-
})
|
|
180
|
-
expect(result.current.hasProAccess).toBe(false)
|
|
181
|
-
expect(result.current.isLoading).toBe(false)
|
|
182
|
-
})
|
|
183
|
-
})
|
package/src/use-subscription.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { useSession } from "next-auth/react";
|
|
2
|
-
|
|
3
|
-
export function useSubscription() {
|
|
4
|
-
const { data: session, status } = useSession();
|
|
5
|
-
|
|
6
|
-
const isLoading = status === "loading";
|
|
7
|
-
const isAuthenticated = status === "authenticated";
|
|
8
|
-
|
|
9
|
-
// Admin kullanıcılar her zaman pro erişime sahip
|
|
10
|
-
const isAdmin = session?.user?.role === "admin";
|
|
11
|
-
|
|
12
|
-
// Pro abonelik kontrolü
|
|
13
|
-
const hasProAccess = isAdmin || session?.user?.subscription?.status === "active";
|
|
14
|
-
const subscriptionPlan = session?.user?.subscription?.plan || (isAdmin ? "lifetime" : "free");
|
|
15
|
-
|
|
16
|
-
// Debug bilgisi
|
|
17
|
-
if (process.env.NODE_ENV === 'development') {
|
|
18
|
-
console.log('🔍 useSubscription Debug:', {
|
|
19
|
-
email: session?.user?.email,
|
|
20
|
-
role: session?.user?.role,
|
|
21
|
-
isAdmin,
|
|
22
|
-
subscription: session?.user?.subscription,
|
|
23
|
-
hasProAccess,
|
|
24
|
-
subscriptionPlan,
|
|
25
|
-
status
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
isLoading,
|
|
31
|
-
isAuthenticated,
|
|
32
|
-
isAdmin,
|
|
33
|
-
hasProAccess,
|
|
34
|
-
subscriptionPlan,
|
|
35
|
-
subscription: session?.user?.subscription,
|
|
36
|
-
};
|
|
37
|
-
}
|