Files
o2o-weather/frontend/tests/linkShare.test.mjs
T
skkim 4fc61d6c02 feat: 날씨 유형 테스트 — 질문 8개로 8유형, 결과별 주소·미리보기, 궁합·잘 맞는 각
- /type 에서 질문 8개에 답하면 날씨 유형 8개 중 하나가 나온다 (/type/{유형})
- 결과를 친구에게 보내면 그 결과의 주소가 가고, 미리보기(제목·설명·그림)가 유형마다 다르다
- 링크로 들어오면 그 결과가 그대로 보이고 '테스트 해 보기'로 이어진다
- 결과마다 다른 유형과의 궁합(찰떡·잘 맞아요·서로 달라서 재밌어요·삐걱삐걱)과
  오늘 잘 맞는 각 3개(풀 6개에서 날짜+유형 기준으로 고정)를 보여 준다
- 오늘 탭 아이콘 줄에 '날씨 유형' 추가, /type 첫 진입은 '날씨 불러오는 중' 덮개를 건너뜀
- GA4 이벤트 7개: type_landing, type_start, type_result, type_share, type_gak, type_match, type_to_today
- 프론트 테스트 추가 (frontend 에서 npm test)
- 유형 그림 8장과 미리보기 그림 9장은 임시 그림 (진짜 그림으로 교체 예정, TODO.md)
- 설계·계획 문서 추가, TODO.md 의 '4주 새 기능 동결' 행 삭제
2026-11-24 08:33:10 +09:00

82 lines
2.9 KiB
JavaScript

// 주소만 보내는 공유(브라우저 공유창 → 안 되면 복사) 검증. 실행: frontend 폴더에서 npm test
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { copyLink, sendLink } from '../src/lib/linkShare.ts'
const URL_ = 'https://weather.o2o.kr/type/sonagi'
const TITLE = '소나기형 · 나는 무슨 날씨일까?'
/** 부른 기록이 남는 가짜 브라우저 */
function fakeNav({ share, write } = {}) {
const calls = { share: [], write: [] }
const nav = {}
if (share)
nav.share = async (data) => {
calls.share.push(data)
return share()
}
if (write)
nav.clipboard = {
writeText: async (text) => {
calls.write.push(text)
return write()
},
}
return { nav, calls }
}
const abort = () => {
throw new DOMException('Share canceled', 'AbortError')
}
const broken = () => {
throw new Error('NotAllowedError')
}
test('공유창이 있으면 제목과 주소만 넘기고 share_sheet', async () => {
const { nav, calls } = fakeNav({ share: () => {}, write: () => {} })
assert.equal(await sendLink(nav, TITLE, URL_), 'share_sheet')
assert.deepEqual(calls.share, [{ title: TITLE, url: URL_ }])
assert.deepEqual(calls.write, [])
})
test('공유창을 그냥 닫으면 cancelled 이고 복사도 하지 않는다', async () => {
const { nav, calls } = fakeNav({ share: abort, write: () => {} })
assert.equal(await sendLink(nav, TITLE, URL_), 'cancelled')
assert.deepEqual(calls.write, [])
})
test('공유창이 오류로 실패하면 주소를 복사하고 copy', async (t) => {
const warn = t.mock.method(console, 'warn', () => {})
const { nav, calls } = fakeNav({ share: broken, write: () => {} })
assert.equal(await sendLink(nav, TITLE, URL_), 'copy')
assert.deepEqual(calls.write, [URL_])
assert.equal(warn.mock.callCount(), 1)
})
test('공유창이 없으면 주소를 복사하고 copy', async () => {
const { nav, calls } = fakeNav({ write: () => {} })
assert.equal(await sendLink(nav, TITLE, URL_), 'copy')
assert.deepEqual(calls.write, [URL_])
})
test('공유창도 클립보드도 없으면 failed', async (t) => {
const warn = t.mock.method(console, 'warn', () => {})
assert.equal(await sendLink(fakeNav().nav, TITLE, URL_), 'failed')
assert.equal(warn.mock.callCount(), 1)
})
test('복사가 막혀 있으면 failed', async (t) => {
const warn = t.mock.method(console, 'warn', () => {})
assert.equal(await sendLink(fakeNav({ write: broken }).nav, TITLE, URL_), 'failed')
assert.equal(warn.mock.callCount(), 1)
})
test('copyLink 는 주소를 복사하고 copy, 안 되면 failed', async (t) => {
const warn = t.mock.method(console, 'warn', () => {})
const ok = fakeNav({ write: () => {} })
assert.equal(await copyLink(ok.nav, URL_), 'copy')
assert.deepEqual(ok.calls.write, [URL_])
assert.equal(await copyLink(fakeNav().nav, URL_), 'failed')
assert.equal(warn.mock.callCount(), 1)
})