Units garrisoning buildings take Panic tests normally – measure distances to the edge of the building. If the test is failed, they flee as described for units that break from combat.
In the same way, a unit that breaks from combat in a building, is destroyed while garrisoning a building, and so on, can trigger Panic tests in friendly units as normal.